ActiveMQspring结合使用
来源:互联网 发布:中文文本挖掘软件 编辑:程序博客网 时间:2024/05/24 22:46
1.
package jms;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
//用来接收信息
public class HelloWorldReciverPS {
public static void main(String args[]){
ApplicationContext context=new FileSystemXmlApplicationContext(
new String[]{"src/applicationContext.xml"});
JmsTemplate jmsTemplate=(JmsTemplate)context.getBean("jmsTemplate");
Destination destination=(Destination)context.getBean("destination");
while(true){
TextMessage msg=(TextMessage)jmsTemplate.receive(destination);
try {
System.out.println("Recieved:"+msg.getText());
} catch (JMSException e) {
System.out.println(e.getMessage());
}
}
}
}
package jms;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
//发送消息
public class HelloWorldSenderPS {
public static void main(String[] args) throws Exception{
ApplicationContext context = new FileSystemXmlApplicationContext(
new String[] {"src/applicationContext.xml"});
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
Destination destination = (Destination) context.getBean("destination");
//取得目的地,有两种方式:queue和topic
for (int i = 0; i < 100; i++) {
System.out.println("Sending Message: Foo"+i);
jmsTemplate.convertAndSend(destination,"Foo "+i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 配置开源ActiveMQ -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
<!-- 采用spring中的jmsTemplate简化jms api-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="connectionFactory"/>
</property>
</bean>
<!-- 发送接收的目的地 -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0">
<value>HelloWorldTopic</value>
</constructor-arg>
</bean>
</beans>
注意:测试时要先启动apache-activemq-5.0.0/bin下的activemq.bat
然后分开个项目启动HelloWorldSenderPS 和HelloWorldReciverPS 。
也可通过程序来启动activemq:
//通过程序启动activemq
BrokerService brokerService=(BrokerService) new BrokerService();
brokerService.addConnector("tcp://localhost:61616");
brokerService.start();
写一个Listener就可以了,让应用启动时就启动了。
通过在spring.xml中文件中配置一直有问题?
- ActiveMQspring结合使用
- GridView,DropDownList结合使用!
- Dwr结合SSH使用
- gallery ,imageSwitcher 结合使用
- spring结合 atomikos 使用
- Selinux结合Apache使用
- find 结合xargs 使用
- LayoutInflater结合Context使用
- CAAnimation结合绘图使用
- TotalCommander结合Bash使用
- List结合ArrayAdapter使用
- Metasploit 结合 proxychains 使用
- nginx结合tomcat使用
- UIScrollView + AutoLayout结合使用
- RecyclerView结合CardView使用
- Elasticsearch结合Nginx使用
- laravel结合datatables使用。
- UIScrollViewAndUIPageController结合使用
- 线程ID 与 线程句柄 区别
- redhat中设置ip地址的几种方法
- Oracle常用资料-1:基础建库资料
- 第5章 浏览器中的JavaScript
- C#中构造函数和析构函数的用法
- ActiveMQspring结合使用
- 中国年味变淡的七大原因
- j2ee简介
- asp.net大小写转换
- 创建线程后为什么关闭了线程句柄,线程还是可以运行?
- 运用postgres常用方法
- asp.net将时间格式化
- asp.net自定义分页代码
- asp.net当文件在不同目录下,需要获取数据库连接字符串