tomcat 整合 activemq (2011-09-06 10:48:38)


在context.xml中添加如下JNDI配置,可以在全局的context.xml加,也可以在具体的某个webapp项目中的context.xml中加:

 name="jms/FailoverConnectionFactory"
 auth="Container"
 type="org.apache.activemq.ActiveMQConnectionFactory"
 description="JMS Connection Factory"
 factory="org.apache.activemq.jndi.JNDIReferenceFactory"
 brokerURL="failover:(tcp://localhost:61616)?initialReconnectDelay=100&maxReconnectAttempts=5"
 brokerName="localhost"
 useEmbeddedBroker="false"/>
 
 name="jms/NormalConnectionFactory"
 auth="Container"
 type="org.apache.activemq.ActiveMQConnectionFactory"
 description="JMS Connection Factory"
 factory="org.apache.activemq.jndi.JNDIReferenceFactory"
 brokerURL="tcp://localhost:61616"
 brokerName="localhost"
 useEmbeddedBroker="false"/>
 
 auth="Container"
 type="org.apache.activemq.command.ActiveMQTopic"
 factory="org.apache.activemq.jndi.JNDIReferenceFactory"
 physicalName="MY.TEST.FOO"/>
 
 auth="Container"
 type="org.apache.activemq.command.ActiveMQQueue"
 factory="org.apache.activemq.jndi.JNDIReferenceFactory"
 physicalName="MY.TEST.FOO.QUEUE"/>

 

physicalName就是JMS应用中用到的destination的名字

 

将ActiveMQ lib目录下的5个jar包复制到Tomcat lib目录下:
activemq-core-5.1.0.jar
activemq-web-5.1.0.jar
geronimo-j2ee-management_1.0_spec-1.0.jar
geronimo-jms_1.1_spec-1.1.1.jar
geronimo-jta_1.0.1B_spec-1.0.1.jar

 

还要加上common-log包。

Listener端(JMSListener.java)完整实现:
package com.flvcd.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.jms.*;

public class JMSListener extends HttpServlet implements MessageListener {
 public void init(ServletConfig config) throws ServletException {
  try {
   InitialContext initCtx = new InitialContext();
   Context envContext = (Context) initCtx.lookup("java:comp/env");
   ConnectionFactory connectionFactory = (ConnectionFactory) envContext.lookup("jms/FailoverConnectionFactory");
   Connection connection = connectionFactory.createConnection();
   connection.setClientID("StevenClient");
   Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   TopicSubscriber consumer = jmsSession.createDurableSubscriber((Topic) envContext.lookup("jms/topic/MyTopic"), "MySub");
   consumer.setMessageListener(this);
   connection.start();
  } catch (NamingException e) {
   e.printStackTrace();
  } catch (JMSException e) {
   e.printStackTrace();
  }
 }

 public void onMessage(Message message) {
  if (checkText(message, "RefreshArticleId") != null) {
   String articleId = checkText(message, "RefreshArticleId");
   System.out.println("接收刷新文章消息,开始刷新文章ID=" + articleId);
  } else if (checkText(message, "RefreshThreadId") != null) {
   String threadId = checkText(message, "RefreshThreadId");
   System.out.println("接收刷新论坛帖子消息,开始刷新帖子ID=" + threadId);
  } else {
   System.out.println("接收普通消息,不做任何处理!");
  }
 }

 private static String checkText(Message m, String s) {
  try {
   return m.getStringProperty(s);
  } catch (JMSException e) {
   e.printStackTrace(System.out);
   return null;
  }
 }
 
}

发送端的实现:

try {
  InitialContext initCtx = new InitialContext();
  Context envContext = (Context) initCtx.lookup("java:comp/env");
  ConnectionFactory connectionFactory = (ConnectionFactory) envContext.lookup("jms/NormalConnectionFactory");
  Connection connection = connectionFactory.createConnection();
  Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  MessageProducer producer = jmsSession.createProducer((Destination) envContext.lookup("jms/topic/MyTopic"));
  
  //设置持久方式
  producer.setDeliveryMode(DeliveryMode.PERSISTENT);
  
  //建立新消息
  //Message testMessage = jmsSession.createMessage();
  ObjectMessage message = jmsSession.createObjectMessage("我在这里");
  
  producer.send(message);
  
  //发布刷新文章消息
  //testMessage.setStringProperty("RefreshArticleId", "9527");
  //producer.send(testMessage);
  //发布刷新帖子消息
  //testMessage.clearProperties();
  //testMessage.setStringProperty("RefreshThreadId", "999");
  //producer.send(testMessage);
 } catch (NamingException e) {
  e.printStackTrace();
 } catch (JMSException e) {
  e.printStackTrace();
 }

 

flex中messaging-config.xml的配置:


    class="flex.messaging.services.MessageService">

    
        
        
    

    
        
    

 
  
   
    true
   

  
         
    topic
    javax.jms.ObjectMessage
    java:comp/env/jms/FailoverConnectionFactory
    java:comp/env/jms/topic/MyTopic
    PERSISTENT
    DEFAULT_PRIORITY
    AUTO_ACKNOWLEDGE
    false
   

        

  
  
 

你可能感兴趣的:(tomcat,mactivemq,消息,java)