Morphium messaging JMS implementation

info

date: 2019-08-20 20:58:00

tags: Java Morphium

category: morphium

Created by: Stephan Bösebeck

logged in

ADMIN


Morphium messaging JMS implementation

Morphium 4.0.6.2 was released this week and this version contains one feature, which I want to show here: Morphium Messaging via JMS!

Here a little proof of concept test from the source:

´´´java

public class BasicJMSTests extends MongoTest {

@Test
public void basicSendReceiveTest() throws Exception {
    JMSConnectionFactory factory = new JMSConnectionFactory(morphium);
    JMSContext ctx1 = factory.createContext();
    JMSContext ctx2 = factory.createContext();

    JMSProducer pr1 = ctx1.createProducer();
    Topic dest = new JMSTopic("test1");

    JMSConsumer con = ctx2.createConsumer(dest);
    con.setMessageListener(message -> log.info("Got Message!"));
    pr1.send(dest, "A test");

    ctx1.close();
    ctx2.close();
}

@Test
public void synchronousSendRecieveTest() throws Exception {
    JMSConnectionFactory factory = new JMSConnectionFactory(morphium);
    JMSContext ctx1 = factory.createContext();
    JMSContext ctx2 = factory.createContext();

    JMSProducer pr1 = ctx1.createProducer();
    Topic dest = new JMSTopic("test1");
    JMSConsumer con = ctx2.createConsumer(dest);

    final Map<String, Object> exchange = new ConcurrentHashMap<>();
    Thread senderThread = new Thread(() -> {
        JMSTextMessage message = new JMSTextMessage();
        try {
            message.setText("Test");
        } catch (JMSException e) {
            e.printStackTrace();
        }
        pr1.send(dest, message);
        log.info("Sent out message");
        exchange.put("sent", true);
    });
    Thread receiverThread = new Thread(() -> {
        log.info("Receiving...");
        Message msg = con.receive();
        log.info("Got incoming message");
        exchange.put("received", true);
    });
    receiverThread.start();
    senderThread.start();
    Thread.sleep(5000);
    assert (exchange.get("sent") != null);
    assert (exchange.get("received") != null);
}

} ´´´

Consider this only being a teaser, it is still BETA or even ALPHA. Not production ready yet.

Unfortunately, some features of morphium won't work using JMS:

  • via JMS you probably can't send direct messages to nodes
  • via JMS you can't youse custom message classes
  • in total, JMS is a lot less flexible than morphium messaging itself it seems. Morphium Messaging allows a lot more details in definition, what message is sent where and how.

But again: this is not production ready, BETA state at best!