队列 主题 生产值 消费者 小案例

本人用的是自己公司项目  maven项目 首先配置pom.xml文件

这个是spring中加的代码 应该还需要context..等  这些就不多说了  用过spring的  还有springmvc的 应该只到还要什么吧  就不一一说了  

<!-- 配置activemq --> <spring.version>4.1.3.RELEASE</spring.version> 
            <dependency>
                <groupId>org.springframework</groupId>  
                <artifactId>spring-jms</artifactId> 
                <version>${spring.version}</version>
            </dependency>
            
            <dependency>
                <groupId>org.apache.xbean</groupId>
                <artifactId>xbean-spring</artifactId>
                <version>3.18</version>
            </dependency>

还有activemq的配置  

<!-- activemq -->
            <!-- 配置activemq -->
            <dependency>  
                <groupId>org.apache.activemq</groupId>  
                <artifactId>activemq-core</artifactId>  
                <version>5.7.0</version>
            </dependency> 
            <dependency>  
                <groupId>org.apache.activemq</groupId>  
                <artifactId>activemq-pool</artifactId>  
                <version>5.12.1</version>  
            </dependency> 

到了这一步 应该是就没有 什么配置了

队列与主题我分两步粘代码 

 

 

 

接下来 我先说 队列的xml的配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd"
        >
    
    <!-- <context:component-scan base-package="com.gzframe.demo.activemq" />
    <mvc:annotation-driven /> -->
        
    <amq:connectionFactory id="amqConnectionFactory" 
        brokerURL="tcp://**.**.**.***:61616" 
        userName="admin" 
        password="admin" />
    
    <!-- 配置JMS连接工长 -->
    <bean id="connectionFactoryMq"
        class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="amqConnectionFactory" />
        <property name="sessionCacheSize" value="100" />
    </bean>
    <!-- 定义队 -->
    <!-- 定义消息队列(Queue) -->
    <bean id="demoQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- 设置消息队列的名字 -->
        <constructor-arg>
            <value>topic.topic.demo2</value>
        </constructor-arg>
    </bean>
    <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
    <bean id="jmsTemplateQueue" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactoryMq" />
        <property name="defaultDestination" ref="demoQueueDestination" />
        <property name="receiveTimeout" value="10000" />
        <!-- true是topic,false是queue,默认是false,此处显示写出false -->
        <property name="pubSubDomain" value="false" />
    </bean>
 </beans>

 

首先我先说 不用监听器的   手动实现生产者消费者

我的controller层

@Controller
public class ActiveMqController {

    @Autowired
    private ActiveMqService activeMqService;//我的service
    /**
     * 
    * @Title: activeTest 
    * @Description: 生产者 队列
    * @param @return    设定文件 
    * @return String    返回类型 
    * @throws
     */
    @RequestMapping("activeTest")
    @ResponseBody
    public String activeTest() {
        activeMqService.activeTest();
        return null;

    }

    
}

 

我的实现层 中间有个接口service  我就不多说了

@Service
public class ActiveMqServiceImpl implements ActiveMqService{
    
    @Resource(name="jmsTemplateQueue")
    private JmsTemplate jmsTemplateQueue;//xml注入过jms的东西  
    //队列名gzframe.demo
    @Resource(name="demoQueueDestination")
    private Destination destination;//mq生成的名字
    
    
    /**
     * 实现生产者
     */
    public void activeTest() {
         System.out.println("向队列" + destination.toString() + "发送了消息------------" + " +++++名字 bug2");
         jmsTemplateQueue.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
              return session.createTextMessage("+++++名字 bug2");
            }
          });
    }
}

实现结果

 

 

现在我开始说 消费者

 

/**
     * 接收消息
     */
    @RequestMapping("receive")
    @ResponseBody
    public String receive() {
        String str = activeMqService.receive();
        System.out.println("成功的取出来了+:" + str);
        return null;

    }

这个方法  是在一个controller里 所以我就不复制类了  实现也是哦!!!

 

 我的实现

 /**
     * 接收消息
     */
    public String receive() {
        TextMessage tm = (TextMessage) jmsTemplateQueue.receive(destination);
        String str = null;
        try {
            System.out.println("从队列" + destination.toString() + "收到了消息:\t"
                    + tm.getText());
            str = tm.getText();
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return str;
    }

实现结果

 

 

我现在 开始监听实现  也就是说 只要我我生产了 东西  直接就会用掉   如果实现监听 我们必须要实现一个接口MessageListener

 首先配置 xml  我就直接监听的xml了  有人用的话 自行在家 之前的xml里

<!-- 显示注入消息监听容器(Queue),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器 -->
    <bean id="queueListenerContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactoryMq" />   //配置工厂
        <property name="destination" ref="demoQueueDestination" />        //监听那个名字
        <property name="messageListener" ref="activeMqServiceImpl" />     //实现监听接口的类
    </bean>

 

@Service
public class ActiveMqServiceImpl implements ActiveMqService,MessageListener{
    
    @Resource(name="jmsTemplateQueue")
    private JmsTemplate jmsTemplateQueue;//xml注入过jms的东西  
    //队列名gzframe.demo
    @Resource(name="demoQueueDestination")
    private Destination destination;//mq生成的名字

    @Override
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
        TextMessage tm = (TextMessage) message;
        try {
            System.out.println("ActiveMqServiceImpl监听到了文本消息:\t"
                    + tm.getText());
            //do something ...
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
    
    
}

这回我只要运行 生产者 就会直接消费了

这个是队列一对一的形式  也有监听  我会在下一篇文章 把我的 主题  订阅模式粘出来   这里的代码比较多了  所以决定放到下一篇文章 哈哈哈

 

posted on 2017-12-29 16:15  bug_锋  阅读(465)  评论(1)    收藏  举报