Activemq SIMPLE Jms example


Introduction to the

Simple Activemp JMS sample code

Activemq run

Simply use Docker to start one:

docker run -dit --name mq -p 11616:61616 -p 8161:8161 rmohr/activemq
Copy the code

Maven dependency Configuration

The dependencies are roughly as follows:


      
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>Against 2.4.1</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>com.example.jms.activemq</groupId>
    <artifactId>jms-activemp</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>jms-activemp</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
Copy the code

Configuration class writing

Configure activemQ related connections as follows:

@Configuration
public class JmsConfig {

    private final String BROKER_URL = "tcp://localhost:11616";
    private final String broker_username = "admin";
    private final String broker_password = "admin";

    @Bean
    public ActiveMQConnectionFactory connectionFactory(a) {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(BROKER_URL);
        connectionFactory.setUserName(broker_username);
        connectionFactory.setPassword(broker_password);
        return connectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplate(a) {
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(connectionFactory());
        return template;
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(a) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setConcurrency("1-1");
        returnfactory; }}Copy the code

Consumer to monitor

Configure the listening consumption of Activemq. If the listening function has a return value, please refer to the following link

@Component
public class JmsConsumer {

    @JmsListener(destination = "activeTest")
    public void receiveMessage(final Map message) { System.out.println(message.toString()); }}Copy the code

producers

Send a message to ActivemQ

@Component
public class JmsProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public void sendMessage(final String topic, final String message) {
        Map map = newGson().fromJson(message, Map.class); jmsTemplate.convertAndSend(topic, map); }}Copy the code

A test run

Send in the main function

@SpringBootApplication
@EnableJms
@Slf4j
public class JmsActivempApplication implements ApplicationRunner {

    @Autowired
    private JmsProducer producer;

    public static void main(String[] args) {
        SpringApplication.run(JmsActivempApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) {
        String topic = "activeTest";
        Map<String, String> message = new HashMap<>(1);
        message.put("test"."test");
        log.info("send message to topic " + topic + "... ""+ message); producer.sendMessage(topic, message); }}Copy the code

Refer to the link

  • Spring Boot ActiveMQ Support