/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package nl.marcenschede.tests; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Resource; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.*; /** * * @author marc */ @MessageDriven(mappedName = "jms/LotusRequest", activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) public class Lotus implements MessageListener { @Resource(mappedName = "jms/LotusReply") private Queue lotusReply; @Resource(mappedName = "jms/LotusReplyFactory") private ConnectionFactory lotusReplyFactory; public Lotus() { } @Override public void onMessage(Message message) { try { TextMessage tm = (TextMessage) message; int code = tm.getIntProperty("code"); int id = tm.getIntProperty("id"); Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Lotus: Customer={0},code={1},id={2}", new Object[]{tm.getText(), String.valueOf(code), String.valueOf(id)}); if(code==0) { sendJMSMessageToLotusReply(tm.getText(), code, id, "OK"); } else { sendJMSMessageToLotusReply(tm.getText(), code, id, "Not OK"); } } catch (JMSException ex) { Logger.getLogger(Lotus.class.getName()).log(Level.SEVERE, null, ex); } } private Message createJMSMessageForjmsLotusReply(Session session, String messageData) throws JMSException { TextMessage tm = session.createTextMessage(); tm.setText(messageData.toString()); return tm; } private void sendJMSMessageToLotusReply(String messageData, int code, int id, String result) throws JMSException { Connection connection = null; Session session = null; try { connection = lotusReplyFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer messageProducer = session.createProducer(lotusReply); Message m = createJMSMessageForjmsLotusReply(session, messageData); m.setIntProperty("id", id); m.setIntProperty("code", code); m.setStringProperty("result", result); messageProducer.send(m); } finally { if (session != null) { try { session.close(); } catch (JMSException e) { Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e); } } if (connection != null) { connection.close(); } } } }