普通类作为JMS消息接收端

public class JMSRequestReply implements MessageListener {
	private InitialContext context;
	private QueueConnectionFactory queueConnectionFactory;
	private QueueConnection queueConnection;
	private QueueSession queueSession;
	private Queue queue;

	public JMSRequestReply() {
		try {
			context = new InitialContext();
			queueConnectionFactory = (QueueConnectionFactory) context
					.lookup("ConnectionFactory");
			// 区别之处
			queue = (Queue) context.lookup("queue/myQueue");
			queueConnection = queueConnectionFactory.createQueueConnection();
			queueSession = queueConnection.createQueueSession(false,
					QueueSession.AUTO_ACKNOWLEDGE);
//			使用QueueReceiver启用监听,而不是会话QueueSession
			QueueReceiver queueReceive = queueSession.createReceiver(queue);
			
			queueReceive.setMessageListener(this);
			
			queueConnection.start();
		} catch (NamingException e) {

			e.printStackTrace();

		} catch (JMSException e) {

			e.printStackTrace();

		}
	}

	public void receive() {

		QueueReceiver queueReceiver;
		QueueSender queuesender = null;
		TextMessage tm = null;
		try {
			System.out
					.println("------------------->i'm a receiver<-------------------");
			queueReceiver = queueSession.createReceiver(queue);
			queueConnection.start();
			// 设置监听类
			// queueReceiver.setMessageListener();
			System.out
					.println("------------------->waiting<-------------------");
			//			ObjectMessage om = (ObjectMessage) queueReceiver.receive();
			//			LogEntry lg = (LogEntry) om.getObject();
			//			System.out.println(lg.getMessage());
			TextMessage tms = (TextMessage) queueReceiver.receive();
			System.out.println(tms.getText());

			// File操作
			// 返回NG、OK
			// return om.getObject();
			queuesender = queueSession
					.createSender((Queue) tms.getJMSReplyTo());
			tm = queueSession.createTextMessage();
			//			 tm.setJMSReplyTo(queue);
			tm.setText("OK");
			queuesender.send(tm);
		} catch (JMSException e) {
			e.printStackTrace();

			try {
				tm.setText("NG");
				queuesender.send(tm);
			} catch (JMSException e1) {
				e1.printStackTrace();
			}

		} finally {
			destroy();
		}
	}

	public String send(LogEntry log) {
		try {
			System.out
					.println("------------------->i'm a sender<-------------------");
			// 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);
			System.out.println("------------------->等待<-------------------");
			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();
		}
	}

	public void onMessage(Message msg) {
		QueueSender queuesender = null;
		TextMessage tm = null;
		try {
			tm = queueSession.createTextMessage();
			tm.setJMSReplyTo(queue);
		System.out.println("------------------->i'm a receiver<-------------------");
			// queueReceiver = queueSession.createReceiver(queue);

			// 设置监听类
			// queueReceiver.setMessageListener();
			ObjectMessage om = (ObjectMessage) msg;
			System.out
					.println("------------------->Listening waiting<-------------------");
			LogEntry lg = (LogEntry) om.getObject();
			System.out.println(lg.getMessage());
			// File操作
			// 返回NG、OK
			// return om.getObject();
//				此处必须这样设置
			System.out.println("目的地:"+ om.getJMSReplyTo()+"比较:----->"+queue);
			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();
			}
		}finally {
			destroy();
		}
	}
}


得到了16:28:34,734 ERROR [STDERR] javax.jms.InvalidDestinationException: This destination does not exist! QUEUE.JMS_TQ7 异常

问题已经解决。

方法:是消息接收端最后不要断开连接,使其一直处于监听状态!

你可能感兴趣的:(jms)