其他分享
首页 > 其他分享> > activemq整合spring之队列生产者

activemq整合spring之队列生产者

作者:互联网

一.applicationContext配置对象

<?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.xsd">

    <context:component-scan base-package="com.aib.mapper"/>


    <!--  配置spring的连接工厂  -->
    <bean id="jmsFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        <property name="targetConnectionFactory">
            <!--  配置activemq原生工厂  -->
            <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://localhost:61616"></property>
            </bean >
        </property>
    </bean >

    <!--  配置topic  -->
    <bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="spring-queue"></constructor-arg>
    </bean >
    <!--  配置queue  -->
    <bean id="activeMQTopic" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg index="0" value="spring-topic"></constructor-arg>
    </bean >
    <!--  配置JMSTemplate  -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory"></property>
        <property name="defaultDestination" ref="activeMQQueue"></property>
        <property name="messageConverter" ref="messageConverter"></property>
    </bean >

    <!--  配置消息的转换器  -->
    <bean id="messageConverter" class="org.springframework.jms.support.converter.SimpleMessageConverter">

    </bean>
</beans>

  JMSTemplate是操作mq的一个对象,即充当生产者也是消费者,都有两者API的功能

  测试JMSTemplate:

public class spring_producer {

    @Test
    public void testJmsTemplate(){

        ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");

        JmsTemplate jmsTemplate = (JmsTemplate) ioc.getBean("jmsTemplate");

        jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {

                TextMessage message = session.createTextMessage("我很牛逼");

                return message;
            }
        });
        System.out.println("发出消息了");
    }

    public static void main(String[] args) throws Exception{

        //使用broker充当mq实例
        BrokerService brokerService = new BrokerService();

        //使用jms协议
        brokerService.setUseJmx(true);
        //获得连接器
        brokerService.addConnector("tcp://localhost:61616");
        //开启连接器
        brokerService.start();
    }
}

 

标签:队列,spring,jmsTemplate,brokerService,连接器,new,activemq,public
来源: https://www.cnblogs.com/ibcdwx/p/14058516.html