0

I'm trying out the sample code from this page to test publishing messages to a WSO2 Message Broker:

https://docs.wso2.com/display/MB310/Sending+and+Receiving+Messages+Using+Queues

The sample code appears to connect -> publish -> disconnect.

I assume this isn't the typical design for production code. I tried reducing the code that is invoked every time I publish a message (several times a second), but it looks like the connection is dropped and never re-established:

public void publishMessage(String msg) throws NamingException, JMSException {
    TextMessage textMessage = topicSession.createTextMessage(msg);
    topicPublisher.publish(textMessage);
}

javax.jms.IllegalStateException: Object org.wso2.andes.client.AMQSession_0_8@79aa1855 has been closed

Do I need to reconnect every time I publish a message?

Mike Stoddart
  • 488
  • 7
  • 21

1 Answers1

1

Samples are only for demonstration purpose. Since JMS is relatively heavy connection it's not recommended to establish connections per message. you can simply modify the code to send multiple message with same session. Based on attached code you can use simple for loop to send 5 messages.

public void publishMessage(String msg) throws NamingException, JMSException {
   TextMessage textMessage = topicSession.createTextMessage(msg);
   for(int i=0; i<5; i++){
        topicPublisher.publish(textMessage);
   }
}
plr
  • 511
  • 3
  • 5
  • 15
  • Thanks - I was doing in this in my code I must be doing something wrong as I appear to be having connection issues. I'll re-read and try again. – Mike Stoddart Oct 05 '16 at 17:22