users@glassfish.java.net

Proper JMS handling

From: <forums_at_java.net>
Date: Wed, 30 Mar 2011 07:29:56 -0500 (CDT)

Hi! I have EJB that sends messages to JMS Queue: private void
sendOutgoingMessage(OutgoingMessageDTO message) throws
OutgoingMessageIsNotCompleteException { try { validateMessage(message);
QueueConnection connection = null; QueueSession session = null; QueueSender
sender = null; try { QueueConnectionFactory qcf = (QueueConnectionFactory)
connectionFactory; connection = qcf.createQueueConnection(); session =
connection.createQueueSession(false, acknowledgeMode); sender =
session.createSender(queue); javax.jms.Message jmsMessage =
session.createObjectMessage(message); sender.send(jmsMessage); } finally {
close(sender, session, connection); } } catch (JMSException ex) {
log.error("Could not send outgoing message to pendragon", ex); } } public
static void close(MessageProducer sender, Session session, Connection conn) {
if (sender != null) { try { sender.close(); } catch (Exception ignore) {} }
if (session != null) { try { session.close(); } catch (Exception ignore) {} }
if (conn != null) { try { conn.close(); } catch (Exception ignore) {} } }
Question is - is this correct way to send JMS, should not it be too heavy to
open close connection (even if it used from pool)? Or should I prepare
Connection in @PostConstruct method of EJB, and reuse it for sending?

--
[Message sent by forum member 'tyutchev_alex']
View Post: http://forums.java.net/node/786622