实现消息队列的两种方式

Apache ActiveMQ官方实例发送消息

直接在Apache官网http://activemq.apache.org/download-archives.html下载ActiveMQ源码

下载解压后拿到java代码实例

\"\" 

然后倒入IDE

如下:

\"\" 

请认真阅读readme.md文件,大致意思就是把项目打成两个jar包,然后启动服务,然后同时运行打的两个jar包,然后就能看到具体的调用信息。打jar包时直接利用maven打就行了,不用修改代码。

启动服务:

\"\" 

\"\"

利用Spring消息模板发送消息

Spirng对Apache ActiveMQ提供了很好的支持

\"\" 

生成者代码:

package com.jms.service.impl;

import com.jms.service.ProducerService;
import org.spring work.jms.core.JmsTemplate;
import org.spring work.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;

/**
 * 发送消息
 */
@Service
public class ProducerServiceImpl implements ProducerService {

 @Resource
 private JmsTemplate jmsTemplate;

 public void sendMessage(Destination destination, String msg) {
  System.out.println(\"向队列\"+destination+\"发送消息\");
  jmsTemplate.convertAndSend(destination,msg);
 }

 public void sendMessage(String msg) {
  System.out.println(\"向队列\"+jmsTemplate.getDefaultDestination().toString()+\"发送消息\");
  jmsTemplate.convertAndSend(msg);
 }
}

消费者代码:

package com.jms.service.impl;

import com.jms.service.CustomerService;
import org.spring work.jms.core.JmsTemplate;
import org.spring work.stereotype.Service;

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

@Service
public class CustomerServiceImpl implements CustomerService {

 @Resource
 private JmsTemplate jmsTemplate;
 /**
  * 接收消息
  * @param destination
  */
 public void receive(Destination destination) {

  TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
  try {
   System.out.println(\"从队列》\"+destination.toString()+\"成功获取消息》\"+textMessage.getText());
  } catch (JMSException e) {
   e.printStackTrace();
  }

 }
}

Spring配置代码

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<beans  ns=\"http://www.spring work.org/schema/beans\"
   ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
   ns:context=\"http://www.spring work.org/schema/context\"
   ns:mvc=\"http://www.spring work.org/schema/mvc\"
  xsi:schemaLocation=\"http://www.spring work.org/schema/beans
  http://www.spring work.org/schema/beans/spring-beans.xsd
  http://www.spring work.org/schema/context
  http://www.spring work.org/schema/context/spring-context.xsd
  http://www.spring work.org/schema/mvc
  http://www.spring work.org/schema/mvc/spring-mvc.xsd
  \">

 <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
 <context:component-scan  -package=\"com.jms.service\"> </context:component-scan>

 <!-- 配置根视图 -->
 <!--<mvc:view-controller path=\"/\" view-name=\"index\"/>-->

 <!--启用注解-->
 <mvc:annotation-driven />

 <!-- 视图层配置 -->
 <!--<bean class=\"org.spring work.web.servlet.view.InternalResourceViewResolver\">-->
  <!--<property name=\"prefix\" value=\"/WEB-INF/view/\"/>-->
  <!--<property name=\"suffix\" value=\".jsp\"/>-->
 <!--</bean>-->


 <!-- 配置JMS连接工厂 -->
 <bean id=\"connectionFactory\" class=\"org.apache.activemq.ActiveMQConnectionFactory\">
  <property name=\"brokerURL\" value=\"tcp://localhost:61616\" />
 </bean>

 <!-- 定义消息队列(Queue) -->
 <bean id=\"queueDestination\" class=\"org.apache.activemq.command.ActiveMQQueue\">
  <!-- 设置消息队列的名字 -->
  <constructor-arg>
   <value>queue1</value>
  </constructor-arg>
 </bean>

 <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
 <bean id=\"jmsTemplate\" class=\"org.spring work.jms.core.JmsTemplate\">
  <property name=\"connectionFactory\" ref=\"connectionFactory\" />
  <property name=\"defaultDestination\" ref=\"queueDestination\" />
  <property name=\"receiveTimeout\" value=\"10000\" />
 </bean>
</beans>

测试代码

package com.jsm.test;

import com.jms.service.CustomerService;
import com.jms.service.ProducerService;
import org.junit.Test;
import org.spring work.context.support.ClassPath ApplicationContext;

import javax.jms.Destination;

/**
 * 消息队列测试类
 */
public class JmsTest {


 @Test
 public void producerTest(){

  ClassPath ApplicationContext springContext = new ClassPath ApplicationContext(new String[]{\"classpath:spring-core. \"});
  ProducerService producerService = (ProducerService)springContext.getBean(\"producerServiceImpl\");
  CustomerService customerService = (CustomerService)springContext.getBean(\"customerServiceImpl\");

  Destination destination = (Destination)springContext.getBean(\"queueDestination\");
  producerService.sendMessage(\"测试消息队列\");
  customerService.receive(destination);
 }
}

测试结果

\"\" 

代码地址

https://github.com/wahnn/SpringJMS
https://gitee.com/wahnn/SpringJMS

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

收藏 打印