使用apache的activemq集合JMS处理异步消息

1:先去apache下载

http://activemq.apache.org/ ,大约有25M,要有点耐心

直接打开就可以使用;当然它也提供了安装为windows service的方法

2:配置activemq

在conf文件夹中配置

activemq.xml

主要是配置内存和端口

<policyEntry queue=">" memoryLimit="100mb"/>
<policyEntry topic=">" memoryLimit="100mb">
<amqPersistenceAdapter syncOnWrite="false" directory="${activemq.base}/data" maxFileLength="100 mb"/>
<memoryUsage limit="50 mb"/>

其中是否异步写是很重要一个参数

3:使用web方式访问管理

http://localhost:8161/admin/

很是方便

4:编程方式使用,系统安装后,有一个DEMO,你可以先测试一下,以后就照这个DEMO写就行了

 

5:在应用中的web.xml配置

<context-param>
<param-name>brokerURI</param-name>
<param-value>/WEB-INF/activemq.xml</param-value>
</context-param>
<listener>
<listener-class>org.activemq.web.SpringBrokerContextListener</listener-class>
</listener>

6:工具类

/**
 *
 */
package MQ;

/**
 * @author 小峰
 *
 */
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.support.JmsGatewaySupport;

public class QSender extends JmsGatewaySupport {

 public static void send(String message) throws JMSException {
  QueueConnectionFactory factory = new ActiveMQConnectionFactory(
    "tcp://127.0.0.1:61616");
  Queue queue = new ActiveMQQueue("queue.name.sample");
  QueueConnection conn = factory.createQueueConnection();
  for (int i = 0; i < 10000; i++) {
   QueueSession session = conn.createQueueSession(false,
     Session.AUTO_ACKNOWLEDGE);
   QueueSender sender = session.createSender(queue);
   TextMessage msg = session.createTextMessage(message);
   sender.send(msg);
  }
  conn.close();
 }
}

 

/**
 *
 */
package MQ;

/**
 * @author 小峰
 *
 */
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

// implements MessageListener in order to receiving asynchronous message
public class QReceiver implements MessageListener {
 // indicate whether stoping receiving
 private boolean stop = false;

 public static void main(String[] args) {
  new QReceiver().receive();
 }

 public void receive() {
  try {
   // the same as QSender
   QueueConnection conn = new ActiveMQConnectionFactory(
     "tcp://127.0.0.1:61616").createQueueConnection();
   Queue queue = new ActiveMQQueue("queue.name.sample");
   QueueSession session = conn.createQueueSession(false,
     Session.AUTO_ACKNOWLEDGE);
   QueueReceiver receiver = session.createReceiver(queue);
   // Sets the message consumer's MessageListener.
   receiver.setMessageListener(this);
   // ready to setup consumer,begin to receive message
   conn.start();
   while (!stop) {
    Thread.sleep(5);
   }
   System.out.println("exiting..");
   conn.close();
  } catch (Exception e) {
   e.printStackTrace();
   System.exit(1);
  }
 }

 // called while receiving message
 @Override
 public void onMessage(Message msg) {
  try {
   String text = ((TextMessage) msg).getText();
   System.out.println(text);
   if (text.equals("stop"))
    stop = true;
  } catch (JMSException e) {
   e.printStackTrace();
   stop = true;
  }
 }
}

 

你可能感兴趣的:(使用apache的activemq集合JMS处理异步消息)