ActiveMQ实例

来源:互联网 发布:电信网络诈骗的类型 编辑:程序博客网 时间:2024/05/22 12:56

1、新建marven项目jms-test


2、在pom.xml导入相关jar包


3、pox.xml配置如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<groupId>com.learn.jms</groupId>
<artifactId>jms-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>


<name>jms-test</name>
<url>http://maven.apache.org</url>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>4.3.12.RELEASE</org.springframework.version>
</properties>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
<exclusions>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>


</dependencies>
</project>


4、新建三个资源文件,common.xml,consumer-jms.xml,producer-jms.xml


common.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config/>

<!-- activeMQ 为我们提供的  ConnectionFactory-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.3.205:61616"/>
</bean>
<!-- spring jms 为我们提供的连接池 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
</bean>
<!-- 一个队列的目的地 点对点 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="cyc_queue_test"></constructor-arg>
</bean>
<!-- 一个主题的目的地 发布订阅 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="cyc_topic_test"></constructor-arg>
</bean>

</beans>


consumer-jms.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 导入公共配置 -->
<import resource="common.xml"/>
<!-- 配置消息监听器 -->
<bean id="consumerMessageListener" class="com.learn.jms.consumer.ConsumerMessageListener"></bean>
<!-- 配置消息监听容器 -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"></property>
<!-- <property name="destination" ref="queueDestination"></property> -->
<property name="destination" ref="topicDestination"></property>
<property name="messageListener" ref="consumerMessageListener"></property>
</bean>

</beans>


producer-jms.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 导入公共配置 -->
<import resource="common.xml"/>

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
</bean>
<bean class="com.learn.jms.producer.ProducerServiceImpl"></bean>
</beans>


5、新建生产者包com.learn.jms.producer


6、接口:ProducerService

package com.learn.jms.producer;



public interface ProducerService {
/**
* 发送消息 
* @param message
*/
void sendMessage(String message);
}

7、生产者实现类ProducerServiceImpl 

package com.learn.jms.producer;


import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;


public class ProducerServiceImpl implements ProducerService {
@Autowired
JmsTemplate jmsTemplate;
@Resource(name = "topicDestination")
// @Resource(name = "queueDestination")
Destination destination;

@Override
public void sendMessage(final String message) {
//使用jmsTemlate 发送消息
jmsTemplate.send(destination,new MessageCreator() {
//创建一个消息
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(message);
return textMessage;
}
});
System.out.println("【发送消息】Message:"+message);
}


}


8、生产者生产消息测试类AppProducer 

package com.learn.jms.producer;


import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AppProducer {
public static void main(String[] args) {
ClassPathXmlApplicationContext  context = new ClassPathXmlApplicationContext("spring/producer-jms.xml");
ProducerService producerService = context.getBean(ProducerService.class);

for (int i = 0; i < 10; i++) {
producerService.sendMessage("test"+i);
}
context.close();
}
}


9、新建消费者包com.learn.jms.consumer


10、消费者监听器ConsumerMessageListener 

package com.learn.jms.consumer;


import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;


public class ConsumerMessageListener implements MessageListener{


@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接受消息"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}

}


}


11、消费者接受消息测试

package com.learn.jms.consumer;


import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AppConsumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext  context = new ClassPathXmlApplicationContext("spring/consumer-jms.xml");
// ConsumerMessageListener producerService = context.getBean(ConsumerMessageListener.class);

}
}

原创粉丝点击