Using the micro broker JMS client

The following is a brief example of how to instantiate and use the micro broker’s JMS client in an Expeditor environment.

Setting project dependencies

You will need the following plug-in dependencies specified in your MANIFEST.MF:

  • com.ibm.mqttclient.jms
  • com.ibm.micro.utils
  • com.ibm.pvc.jms

Creating your JMS Connection Factory programmatically

The micro broker provides a factory (a “factory factory”!) for creating JMS Connection Factory instances. This factory is accessed as follows:

JmsFactoryFactory jmsFactory = JmsFactoryFactory.getInstance(MQTTConstants.PROVIDER_NAME);

// Obtain a connection factory
ConnectionFactory cf = jmsFactory.createConnectionFactory();
// We need to set the target endpoint on the connection factory
// Connecting to localhost over TCP
((JmsConnectionFactory) cf).setStringProperty(MQTTConstants.MQTT_CONNECTION_URL,MQTTConstants.MQTT_TCP_SCHEMA + "127.0.0.1:1883");

Note that we set some specific properties for MQTT, such as the connection URL. A client ID is mandatory for the micro broker JMS client too. Connection Factories can be created declaratively using an Expeditor Extension Point. Details of how to do this can be found in the Expeditor Info Center on this page.

Creating a JMS Connection

The main point to note here is that the micro broker requires a client ID to be specified.

// Now obtain a connection from the factory
Connection conn = cf.createConnection();
// A client ID is required
conn.setClientID("Pinger");
// Connection is ready for use.

A sample JMS application

// Connection Factory
JmsFactoryFactory jmsFactory = JmsFactoryFactory.getInstance(MQTTConstants.PROVIDER_NAME);
// Obtain a connection factory
ConnectionFactory cf = jmsFactory.createConnectionFactory();
// We need to set the target endpoint on the connection factory
// Connecting to localhost over TCP
((JmsConnectionFactory) cf).setStringProperty(MQTTConstants.MQTT_CONNECTION_URL, MQTTConstants.MQTT_TCP_SCHEMA + "127.0.0.1:1883");
...
// Now obtain a connection from the factory
Connection conn = cf.createConnection();
// A client ID is required
conn.setClientID("Pinger");
// Finally start the connection up
conn.start();
...
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session.createTopic(MICROBROKER_TOPIC_NAME_OUTBOUND));
MessageConsumer consumer = session.createConsumer(session.createTopic(MICROBROKER_TOPIC_NAME_INBOUND));
...
TextMessage requestMsg = session.createTextMessage("{ \"telephone\" : \"04962 915000\" }");
requestMsg.setStringProperty("TargetFunctionName", "checkDSL");
producer.send(requestMsg);
...
Message responseMsg = (Message) consumer.receive(JMS_REQUEST_TIMEOUT);
...
session.close();
conn.stop();
conn.close();
Advertisements

10 responses to “Using the micro broker JMS client

  1. Hi Martin,

    I have code working properly..

    Microbroker is working perfectly.

    I am able to put a Message from JMS Client to Microbroker and Microbroker is successfully sending the message to MQ

    My Problem is :-

    JMSClient Client is not able to get a message from microbroker but Microbroker is able to get a meesage from MQ.

    Can u please assist me on the same?

    Regards,
    -Manjunath
    South Africa
    GijimaAst

    • Hi Manjunath,

      From your comment, it sounds like you see the message being removed from MQ by the micro broker but you aren’t seeing it where you expect it to in the micro broker side. You might want to check your inbound flow definition on the pipe you created within the micro broker — it should be putting the message from MQ into a queue or topic in the micro broker as defined in the flow defintion, the reverse of what you did (successfully) to pass the message from the micro broker to MQ in the first place. Make sure that you are consuming from the right destination queue or topic within the micro broker and that your outbound flow isn’t reading from the topic or queue that the messages from MQ are being put to.

      I hope that helps,
      Martin

  2. Hi Martin,
    Below is Inbound flow definition .

    FlowDefinition inboundflowQueue = bridge.createFlowDefinition(“flow”);
    inboundflowQueue.setSources(new QueueDefinition[] { bridge.createQueueDefinition(SOURCE_MQ_Q) });
    inboundflowQueue.setTarget(bridge.createQueueDefinition(“targetQ”));
    inboundflowQueue.setQos(0);
    pipeDef.addInboundFlow(inboundflowQueue);

    In JMS Client to receive message from Microbroker.

    MessageConsumer cons = sess.createConsumer(targetQ)
    TextMessage recvMess = (TextMessage)cons.receive();

    Microbroker Picks message MQ but JMS Client is not able to get the same from Micrboker.

    Please let me know if anything is incorrect in the above code and anything I am missing.

    Regards,
    -Manjunath

    • Hi Manjunath,

      It’s interesting that you are seeing the message move out of MQ (and one would expect into the micro broker, unless there is another application reading messages off the same queue). From what I can see, I would suggest the following:

      Verify the queue “targetQ” has been created in your micro broker — you have to create them administratively using the administrative API. You could always put the message to a topic (requires no administrative action) and subscribe rather than use a queue perhaps.
      I would remove the QoS setting on the inbound flow — it is not really meaningful in this context.
      Verify that your targetQ object in the JMS app is indeed pointing at the queue named “targetQ”.
      Check that the inbound queue is not full. Again, using a topic might be an easier approach for testing.

      One approach for development is to use the Equinox-based test environment in Eclipse — you will see the micro broker’s console log directly into the console. Other than the above, I cannot see from the snippets anything obvious, and as you say the message is being moved from MQ as you expect. If you’re still stuck, it would be worth contacting your IBM representative so they can take a closer look.

      Thanks
      Martin

  3. Hi Manjunath,
    I think you can have a look at the draft Redbook (http://www.redbooks.ibm.com/abstracts/tips0684.html?Open) where we have clearly explained how you can get and put messages from JMS clients. Chapter 3 and 5 has code that can help.
    Regards,
    Neeraj Krishna
    IBM Software Labs

  4. Chris Aldrich

    Thanks much for your posting on how to create a sample JMS application. I have the IBM Client for Java Message Service on Java 2 Platform, Standard Edition with IBM WebSphere Application Server jar file (sibc.jms.jar) from IBM to work with WAS. And I’ve been smashing my head against my desk for a few days now, looking for documentation on how to use this thing. Your classes seem to be tailored more for MQ, but they still helped out greatly. I’ve actually been able to get my client to connect to my local WebSphere instance and put a message on a queue. Thanks a bundle for this! You’ve saved my life!

    Chris Aldrich

  5. Brian Fennern

    We are researching upgrade options for our aging MQ Series / Integrator based system. Our system has QOS0 and QOS1 MQTT messages coming in. The QOS0 messages are republished back to an MQTT output node. The QOS1 messages are passed to a Oracle stored procedure. Using the micro broker, is it possible to place the message payload from a MQTT inbound flow into a queue and then move the data from the queue to a Oracle system with a stored procedure? Can the queue be accessed through a JMS connection? Once the QOS1 data is in the queue, what options or connections are available for accessing it? What are the limitations of the Expeditor micro broker starter edition? Does anyone know of integrators in the states with solid micro broker experience?

    Thanks,
    Brian Fennern

  6. We are trying to setup a bridge between microbroker and websphere mq 7.0 and it seems to setup the bridge but when the broker is started..i get the class not found exception (I even put the websphere mq jars in the broker classpath)

    Fri Feb 19 16:00:18 EST 2010 I FMBE1200 FirstBroker ————–MicroBroker Starting————–
    Fri Feb 19 16:00:18 EST 2010 I FMBE1201 FirstBroker MicroBroker version 3.0.1.0 – ub000-L090807.2.
    Fri Feb 19 16:00:19 EST 2010 I FMBB2006 bridge Pipe BrokerMQPipe initializing.
    Fri Feb 19 16:00:19 EST 2010 I FMBB4036 bridge Pipe ‘BrokerMQPipe’ setting transmission control policy ‘AlwaysConnectedConnectionPolicyDefinition’.
    Fri Feb 19 16:00:19 EST 2010 I FMBB2004 bridge Pipe BrokerMQPipe starting.
    Fri Feb 19 16:00:19 EST 2010 I FMBD1124 FirstBroker Listening on port 1883
    Fri Feb 19 16:00:19 EST 2010 I FMBB4037 bridge Pipe ‘BrokerMQPipe’ started transmission control.
    Fri Feb 19 16:00:19 EST 2010 I FMBB2000 bridge Pipe BrokerMQPipe started.
    Fri Feb 19 16:00:19 EST 2010 I FMBE1210 FirstBroker MicroBroker started.
    Exception in thread “MicroBroker Bridge Reconnector” java.lang.NoClassDefFoundError: com.ibm.mq.jms.MQConnectionFactory
    at com.ibm.micro.internal.bridge.connection.jms.mq.JMSMQBridgeConnection.initialiseConnectionFactory(JMSMQBridgeConnection.java:120)
    at com.ibm.micro.internal.bridge.connection.jms.common.JMSBridgeConnection.initialise(JMSBridgeConnection.java:201)
    at com.ibm.micro.internal.bridge.connection.jms.common.JMSBridgeConnection.init(JMSBridgeConnection.java:184)
    at com.ibm.micro.internal.bridge.BridgeProvider.startConnector(BridgeProvider.java:1282)
    at com.ibm.micro.internal.bridge.Pipe.openOutboundConnection(Pipe.java:2542)
    at com.ibm.micro.internal.bridge.Pipe$ReconnectThread.run(Pipe.java:1519)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s