Skip to main content

Fighting network latency : howto cache a JMS Connection/Session/MessageProducer?

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
dcausse
Offline
Joined: 2013-06-07

Hi,
we use glassfish with a REMOTE JMS broker. When we were running in EMBEDDED we did not experienced this issue but now we do. Network latency adds a bad delay for each Connection/Session/Producer creation it's why we would like to cache those object and reuse them as much as possible.

Here is a code snippet to illustrate what I would like to do.
My question is : is it safe to do that, does connection/session in glassfish have been designed to be kept in possible long lived EJB?

@Stateful
public class ARSenderBean {
@EJB
private LegacyCorpusManagerAutoRecolte cman;

@Resource(mappedName = Messaging.LOCAL_JNDI_CONN_FACTORY)
private ConnectionFactory connectionFactory;

@Resource(mappedName = AutoRecolteIndexerConsumer.QUEUE_NAME)
private Queue arQueue;

@Resource(mappedName = LegacyDocumentARCommonConsumer.FILTER_QUEUE_NAME)
private Queue filterQueue;
@Resource(mappedName = AliasToCreateWorkflowConsumer.QUEUE_NAME)
private Queue atcQueue;

private Connection connection;
private Session session;
private MessageProducer arProd;
private MessageProducer filterProd;
private MessageProducer atcProd;

/**
* Initialize JMS objects
*/
@PostConstruct
public void init() {
try {
connection = connectionFactory.createConnection(); // 1-2 ms
session = connection.createSession(true, Session.SESSION_TRANSACTED); // 1-2 ms
arProd = session.createProducer(arQueue); // 1-2 ms
filterProd = session.createProducer(filterQueue); // 1-2 ms
atcProd = session.createProducer(atcQueue); // 1-2 ms
// total of 6 or 8 ms

arProd.setDisableMessageID(true);
filterProd.setDisableMessageID(true);
atcProd.setDisableMessageID(true);
} catch(JMSException jmse) {
throw new EJBException(jmse);
}
}

/**
* Return the connection to the pool
*/
@PreDestroy
public void destroy() {
try {if(connection != null) connection.close();} catch(JMSException jmse) {}
}

public void sendFilter(FilterMessage msg) {
try {
filterProd.send(msg.getJMSMessage(session));
} catch(JMSException jmse) {
throw new EJBException(jmse); // Shoud destroy this EJB instance and call PreDestroy?
}
}

public void sendIndex(AutoRecolteIndexerMessage msg) {
try {
arProd.send(msg.getJMSMessage(session));
} catch(JMSException jmse) {
throw new EJBException(jmse); // Shoud destroy this EJB instance and call PreDestroy?
}
}

public void sendAtc(LegacyDocumentDataMessage msg) {
try {
atcProd.send(msg.getJMSMessage(session));
} catch(JMSException jmse) {
throw new EJBException(jmse); // Shoud destroy this EJB instance and call PreDestroy?
}
}
}

Thanks