20090601
来源:互联网 发布:网络直播歌手排名 编辑:程序博客网 时间:2024/05/01 05:07
tomcat 整合 activemq
在context.xml中添加如下JNDI配置,可以在全局的context.xml加,也可以在具体的某个webapp项目中的context.xml中加:
<Resource
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"/>
<Resource
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"/>
<Resource name="jms/topic/MyTopic"
auth="Container"
type="org.apache.activemq.command.ActiveMQTopic"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="MY.TEST.FOO"/>
<Resource name="jms/queue/MyQueue"
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的配置:
<?xml version="1.0" encoding="UTF-8"?>
<service id="message-service"
class="flex.messaging.services.MessageService">
<adapters>
<adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true" />
<adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/>
</adapters>
<default-channels>
<channel ref="my-polling-amf"/>
</default-channels>
<destination id="chat">
<properties>
<server>
<durable>true</durable>
</server>
<jms>
<destination-type>topic</destination-type>
<message-type>javax.jms.ObjectMessage</message-type>
<connection-factory>java:comp/env/jms/FailoverConnectionFactory</connection-factory>
<destination-jndi-name>java:comp/env/jms/topic/MyTopic</destination-jndi-name>
<delivery-mode>PERSISTENT</delivery-mode>
<message-priority>DEFAULT_PRIORITY</message-priority>
<acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
<transacted-sessions>false</transacted-sessions>
</jms>
</properties>
<adapter ref="jms"/>
</destination>
</service>
- 20090601
- Linux操作系统下C语言编程入门学习笔记(...20090601(第一遍学完待整理)...)
- [文摘20090601]美国和中国老师讲灰姑娘的故事(差距啊~体现得淋漓尽致)
- 第一次上,发个帖子,哈哈O(∩_∩)O哈!
- Convert string type to int
- zk组件开发指南(1)
- WebDAV Detection, Vulnerability Checking and Exploitation
- 疯狂的机器人-软件
- 20090601
- 关于用友的CELL控件
- 信用卡套现风险骤然积聚 工行深发展深陷其中
- PHP学习的第一天
- Watin1.3 release
- J2ME 解析wml数据的例子
- Oracle性能诊断的方法
- 网站提升流量的宝贵经验
- ORACLE问题诊断思路