public String send(LogEntry log) {
try {
// QueueSender queuesender = queueSession.createSender(queue); QueueRequestor queueRequestor = new QueueRequestor(queueSession, queue); queueConnection.start();
ObjectMessage ob = queueSession.createObjectMessage(log);
// TextMessage txs = queueSession.createTextMessage();
// txs.setText("I'am zhuyefeng !it's true");
TextMessage reply = (TextMessage) queueRequestor.request(ob);
String replys = reply.getText();
return replys; }
catch (JMSException e)
{ // TODO Auto-generated catch block
e.printStackTrace();
return null; }finally{ destroy(); } }
public void destroy() {
try {
queueConnection.close();
queueSession.close(); }
catch (JMSException e)
{ // TODO Auto-generated catch block
e.printStackTrace(); } }
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/myQueue") }) @TransactionManagement(TransactionManagementType.BEAN)
public class ReplyMDB implements MessageListener { public void onMessage(Message msg) {
InitialContext context;
QueueConnectionFactory queueConnectionFactory;
QueueConnection queueConnection = null; QueueSession queueSession = null; Queue queue = null;
QueueSender queuesender = null ; TextMessage tm = null ;
try { context = new InitialContext();
queueConnectionFactory = (QueueConnectionFactory) context .lookup("ConnectionFactory");
queue = (Queue) context.lookup("queue/myQueue");
queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start();
queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
tm = queueSession.createTextMessage(); tm.setJMSReplyTo(queue);
// queueReceiver = queueSession.createReceiver(queue);
ObjectMessage om = (ObjectMessage) msg;
LogEntry lg = (LogEntry) om.getObject();
queuesender = queueSession.createSender((Queue)om.getJMSReplyTo()); tm.setText("OK"); queuesender.send(tm); }
catch (JMSException e) {
e.printStackTrace();
try { tm.setText("NG"); queuesender.send(tm); }
catch (JMSException e1) { e1.printStackTrace(); } }
catch (NamingException e) { e.printStackTrace(); }
finally{ try { queueConnection.close(); queueSession.close(); }
catch (JMSException e) { e.printStackTrace(); } } }
不使用MDB时 ,产生javax.jms.InvalidDestinationException :This destination does not exist!异常,原因调查中!