基于JavaConfig配置,整合Spring和ActiveMQ

2 篇文章 0 订阅
1 篇文章 0 订阅

pom.xml

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>4.3.7.RELEASE</org.springframework.version>
    </properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-pool</artifactId>
        <version>5.16.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
</dependencies>

说明: junit版本注意下,测试报要求4.12版本的时候记得回来看一眼

创建Config类

package com.😊.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.SimpleMessageConverter;

@ComponentScan({"com.😊"})
@Configuration
public class ActiveMQConfig {
    @Bean
    public PooledConnectionFactory getPooledConnectionFactory(){
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://你ActiveMQ的ip地址:61616");
        PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(activeMQConnectionFactory);
        pooledConnectionFactory.setMaxConnections(100);
        return pooledConnectionFactory;
    }

    @Bean("activeMQQueue")
    public ActiveMQQueue getActiveMQQueue(){
        ActiveMQQueue activeMQQueue = new ActiveMQQueue("spring-active-queue");
        return activeMQQueue;
    }

    @Bean("activeMQTopic")
    public ActiveMQTopic getActiveMQTopic(){
        ActiveMQTopic activeMQTopic = new ActiveMQTopic("spring-active-topic");
        return activeMQTopic;
    }

    @Bean("jmsTemplate")
    public JmsTemplate getJmsTemplate(){
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(getPooledConnectionFactory());
//        jmsTemplate.setDefaultDestination(getActiveMQQueue());
        jmsTemplate.setDefaultDestination(getActiveMQTopic());
        SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();
        jmsTemplate.setMessageConverter(simpleMessageConverter);
        return jmsTemplate;
    }
}

说明: getActiveMQQueue() 方法是创建队列的,getActiveMQTopic() 方法是创建Topic的,

只需要在jmsTemplate.setDefaultDestination(getActiveMQTopic()); 中指定就行

注意在类上面加注解:
@ComponentScan({“com.😊”}) //扫描包
@Configuration //表明这是一个配置

生产者

package com.😊.activemq;

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

import javax.jms.TextMessage;

@Controller
public class SpringMQ_Produce {
    @Autowired
    public JmsTemplate jmsTemplate;

    public void createProduce() {
        jmsTemplate.send(session -> {
            TextMessage textMessage = session.createTextMessage("这是一个消费者");
            return textMessage;
        });
    }
}

说明: jmsTemplate 就是在配置类中配置的,启动的时候会去读取配置类,完成配置类中bean的创建,在这里只需要使用 @Autowired 自动注入进来

消费者

package com.😊.activemq;

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

import javax.jms.JMSException;
import javax.jms.TextMessage;

@Controller
public class SpringMQ_Consumers {
    @Autowired
    JmsTemplate jmsTemplate;

    public void  createConsumer() throws JMSException {
        TextMessage textMessage = (TextMessage)jmsTemplate.receive();
        System.out.println(textMessage.getText());
    }
}

说明:在这里只需要获取消息就可以了

编写测试类

在生成者或消费者的方法中右键,然后GO TO,选择Test
在这里插入图片描述
说明: 记得把Member勾上

测试类

package com.😊.activemq;

import com.ysf.config.ActiveMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.jms.JMSException;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ActiveMQConfig.class)
public class SpringMQ_ProduceTest {
    @Autowired
    SpringMQ_Produce springMQ_produce;

    @Autowired
    SpringMQ_Consumers springMQ_consumers;

    @Test
    public void createProduce() {
        springMQ_produce.createProduce();
    }

    @Test
    public void createConsumer() throws JMSException {
        springMQ_consumers.createConsumer();
    }

}

说明:
@RunWith(SpringJUnit4ClassRunner.class) 就这样写
@ContextConfiguration(classes = ActiveMQConfig.class) 这个是设置配置类的,就是一 开始创建 的配置类

@Autowired
SpringMQ_Produce springMQ_produce;

因为SpringMQ_ProduceSpringMQ_Consumers都用注解 @Controller 标注了,扫描到的时候就会创建实例,这里直接注入就行

这里写了两个测试方法,一个模拟生产者,一个模拟消费者,在Queue的时候是可以的,
但是在Topic的时候也能成功,但是看不到控制台打印消息,可以再写一个测试类(要注意在Topic模式下,要先启动消费者(订阅者))

PS:写配置类的时候,可以对照着基于xml的配置,可以加深你对Spring的依赖注入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值