Mike Stoddart
Mike Stoddart

Reputation: 506

Managing connection to WSO2 Message Broker

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?

Upvotes: 0

Views: 91

Answers (1)

plr
plr

Reputation: 511

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);
   }
}

Upvotes: 1

Related Questions