1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
public class JmsUtil {
private Message createJMSMessageForjmsMyQueue(Session session, Object messageData) throws JMSException {
// TODO create and populate message to send
TextMessage tm = session.createTextMessage();
tm.setText(messageData.toString());
return tm;
}
private void sendJMSMessageToMyQueue(Object messageData) throws NamingException, JMSException {
Context c = new InitialContext();
ConnectionFactory cf = (ConnectionFactory) c.lookup("jms/myQueueFactory");
Connection conn = null;
Session s = null;
try {
conn = cf.createConnection();
s = conn.createSession(false, s.AUTO_ACKNOWLEDGE);
Destination destination = (Destination) c.lookup("jms/myQueue");
MessageProducer mp = s.createProducer(destination);
mp.send(createJMSMessageForjmsMyQueue(s, messageData));
} finally {
if (s != null) {
try {
s.close();
} catch (JMSException e) {
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
}
}
if (conn != null) {
conn.close();
}
}
}
} |
Partager