user1557197
user1557197

Reputation: 21

Non-blocking Jms Queue Sender

I want to publish log/audit events to JMS queue server. I implemented "QueueConnectActor" which constructs the message, creates queue and send message using following code.

This makes blocking call to JMS. I am wondering is there any better non-bloking way to send messages to queue? In other words any reference/pointers or sample code for jms client on playframework.

    QueueConnectionFactory factory = new com.tibco.tibjms.TibjmsQueueConnectionFactory(serverUrl);
    QueueConnection connection = factory.createQueueConnection(userName,password);
    QueueSession session = connection.createQueueSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);

    javax.jms.Queue queue = session.createQueue(queueName);
    QueueSender sender = session.createSender(queue);        

    javax.jms.TextMessage textMessage = session.createTextMessage();
    textMessage.setText(eventXml);
    sender.send(textMessage);

    connection.close();

Thanks!

Upvotes: 1

Views: 2098

Answers (1)

Shashi
Shashi

Reputation: 15263

JMS 1.1 Specification does not specify an API or options for a non-blocking send call. So JMS implementations will not have non-blocking aka Asynchronous Send feature. However JMS implementations like WebSphere MQ have provider specific options that to send messages using a non-blocking send call. (See sample code below)

Recently (a year back or so), JMS 2.0 specification has added a new method to allow an application to send messages asynchronously.

Below sample code demonstrates Asynchronous send using WebSphere MQ.

  connection = cf.createConnection();
  session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  destination = session.createQueue("queue:///Q1");

  // Request asynchronous sends to this destination. Note: Message will be 
  // sent asynchronously if the environment permits; otherwise, message will 
  // be sent synchronously. See documentation for further details.
  ((JmsDestination) destination).setIntProperty(WMQConstants.WMQ_PUT_ASYNC_ALLOWED,
      WMQConstants.WMQ_PUT_ASYNC_ALLOWED_ENABLED);

  producer = session.createProducer(destination);

  long uniqueNumber = System.currentTimeMillis() % 1000;
  TextMessage message = session
      .createTextMessage("SimpleAsyncPutPTP: Your lucky number today is " + uniqueNumber);

  // Start the connection
  connection.start();

  // And, send the message
  producer.send(message);
  System.out.println("Sent message:\n" + message);

Upvotes: 2

Related Questions