springboot learning notes

(VI) springboot integrates activemq

1. Now download ActiveMQ. Download link: http://activemq.apache.org/download.html. After the windows system is decompressed, enter the bin directory. It is divided into 32-bit and 64 bit operating systems. Run the activemq.bat startup program. Visit http://http://127.0.0.1:8161

 

The interface appears to indicate that the installation is successful. Click broker, enter the account admin password admin to enter the management interface

Click the queue button to create a message queue.

2. Add the following dependencies in pom file, and configure activema in application.properties

 

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

 

#Access address
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
#Open thread pool or not
spring.activemq.pool.enabled=true
#maximum connection
spring.activemq.pool.max-connections=50

3. Add the following code in Application.class to facilitate injection into the queue

 

package com.zc.app.test;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class TestApplication {
    @Bean
    public Queue queue(){
        return new ActiveMQQueue("test.queue");
    }
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}
TestApplication

 

4. Create a new service interface to send messages and implement sending messages

 

/**
 * 
 */
package com.zc.app.test.service;

import javax.jms.Destination;


public interface MsgService {

    public void sendMessage(Destination destination,String message);
    
    public void sendMessage(String message);
}
MsgService

 

/**
 * 
 */
package com.zc.app.test.service.impl;



import javax.jms.Destination;
import javax.jms.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import com.zc.app.test.service.MsgService;



@Service
public class MsgServiceImpl implements MsgService{
    

    @Autowired
    private Queue queue;
    
    @Autowired
    private JmsMessagingTemplate jms; //Used to send messages

    @Override
    public void sendMessage(Destination destination,String message) {
        jms.convertAndSend(this.queue, message);
        
    }

    @Override
    public void sendMessage(String message) {
        jms.convertAndSend(message);
        
    }

    

}
MsgServiceImpl

Write controller to call interface

 

/**
 * 
 */
package com.zc.app.test.controller;

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zc.app.test.service.MsgService;


@RestController
@RequestMapping("/msg")
public class SendController {

    @Autowired
    private MsgService msgService;
    
    @GetMapping("send")
    public String order(String message) {
        
        Destination destination = new ActiveMQQueue("test.queue");
        msgService.sendMessage(destination, message);
        return "send message success";
    }
}
SendController

 

6. Start the program. At this time, an error appears in the program, indicating that JmsMessagingTemplate injection failed

 

 

 

Field jms in com.zc.app.test.service.impl.MsgServiceImpl required a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' that could not be found.

 

7. Through the test, after the acticemq thread pool configuration is deleted, the program can start normally. Finally, the reason is that there is no jms pool dependency package. After adding the following dependencies to the pom file, the program can start normally

 

        <dependency>
            <groupId>org.messaginghub</groupId>
            <artifactId>pooled-jms</artifactId>
        </dependency>

 

8. Then visit url: localhost:8080/msg/send?message=124515. After the prompt is successful, you can see the increase of test.queue message from the management interface of activemq

Tags: Java Spring Apache SpringBoot Windows

Posted on Mon, 02 Dec 2019 10:28:26 -0500 by shamly