activemq ajax 接收消息报错IllegalStateException: IDLE,initial

Defens 发布于 2015/12/11 19:52
阅读 629
收藏 0

最近因为项目需要研究ActiveMQ,主要是集成在管理后台的网页中,使用ajax接收消息,已达到消息的实时送达的效果。但是在测试中发现以下的一些异常无法解决,上来问问:

发送消息代码如下:

public class MessageSender {

	public static void sendMessage(String messgaeStr, String receiver) {

		Connection connection = JMSConnectionUtill.getInstance()
				.getConnection();
		// Session: 一个发送或接收消息的线程
		Session session = null;

		// Destination :消息的目的地;消息发送给谁.
		Destination destination = null;
		// MessageProducer:消息发送者
		MessageProducer producer = null;
		TextMessage message = null;

		// 启动
		try {
			connection.start();
			session = connection.createSession(Boolean.TRUE,
					Session.AUTO_ACKNOWLEDGE);    //自动确认
			destination = session.createQueue(receiver);
			producer = session.createProducer(destination);
			producer.setDeliveryMode(DeliveryMode.PERSISTENT);
			message = session.createTextMessage();
			message.setText(messgaeStr);
			producer.send(message);
			session.commit();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args){
		MessageSender.sendMessage("<message>sssssssssssssssss</message>", "test");
	}
}

获取连接工具类:

public class JMSConnectionUtill {
	
	private PooledConnectionFactory pooledConnectionFactory = null;
	
	private static class CreateConnUtil{
		private  static JMSConnectionUtill instance = new JMSConnectionUtill();
	}

	private JMSConnectionUtill (){
		// ConnectionFactory :连接工厂,JMS 用它创建连接
		ActiveMQConnectionFactory connectionFactory = null;
		// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
		connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
		
		pooledConnectionFactory = new PooledConnectionFactory();
		pooledConnectionFactory.setConnectionFactory(connectionFactory);
		//设置最大连接数
		pooledConnectionFactory.setMaxConnections(200);
		//设置最小
		pooledConnectionFactory.setMaximumActiveSessionPerConnection(50);     
		//后台对象清理时,休眠时间超过了3000毫秒的对象为过期
		pooledConnectionFactory.setTimeBetweenExpirationCheckMillis(3000);
	}  
	
	//获取链接
	public Connection getConnection(){
		System.out.println(pooledConnectionFactory);
		Connection connection = null;
		try {
			connection = pooledConnectionFactory.createConnection();
		} catch (JMSException e) {
			System.out.println("PooledConnectionFactory获取连接失败");
			e.printStackTrace();
		}
		return connection;
	}
	
	public static final JMSConnectionUtill getInstance(){
		return CreateConnUtil.instance;
	}
}



网页端:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Test</title>
    
  <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
  <script type="text/javascript" src="js/amq_jquery_adapter.js"></script>
  <script type="text/javascript" src="js/amq.js"></script>
  
  <script type="text/javascript">
    $(document).ready(
	    function()
	    {
	      org.activemq.Amq.init(
	       { 
	         uri: 'amq', 
	         logging: true, 
	         timeout: 1,
	         clientId:(new Date()).getTime().toString()
	       }
	      );
	    });
    </script>
</head>

<body>
  <h2>Hello World!</h2>
</body>
  <script type="text/javascript">
  
    var amq = org.activemq.Amq;
  
    var myHandler =
      {
        rcvMessage: function(message)
        {
           alert(message.childNodes[0].data);
        }
      };
    $(document).ready(
      function()
      {
        amq.addListener("testHandlerId","queue://test",myHandler.rcvMessage);
      });
  </script>
</html>



问题描述:

1、必须是接收端先启动,否则消息不会送达,但是通过ActiveMQ的控制台却看到服务被消费了;

2、每次舒心一个页面,消费者多一个,关闭浏览器依然是的,只有运行这个网页的项目停了才会消除

3、消息前一两次会送达,发送消息端发送超过三次就会报错,报错信息如下:

WARN | Error receiving message IDLE,initial. This exception is ignored.
java.lang.IllegalStateException: IDLE,initial
	at org.eclipse.jetty.server.AsyncContinuation.dispatch(AsyncContinuation.java:408)
	at org.eclipse.jetty.server.AsyncContinuation.resume(AsyncContinuation.java:815)
	at org.apache.activemq.web.AjaxListener.onMessageAvailable(AjaxListener.java:71)
	at org.apache.activemq.ActiveMQMessageConsumer.dispatch(ActiveMQMessageConsumer.java:1415)
	at org.apache.activemq.ActiveMQSessionExecutor.dispatch(ActiveMQSessionExecutor.java:131)
	at org.apache.activemq.ActiveMQSessionExecutor.iterate(ActiveMQSessionExecutor.java:202)
	at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:133)
	at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
 WARN | Error receiving message IDLE,initial. This exception is ignored.

希望有了解过的大神能够给点建议和解决思路,如果有已经成功的源码可以参考,感激不尽。





加载中
返回顶部
顶部