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
中文件中配置一直有问题?