Home > Java > javaTutorial > How to integrate ActiveMQ in SpringBoot

How to integrate ActiveMQ in SpringBoot

王林
Release: 2023-05-11 18:16:06
forward
1334 people have browsed it

Directory structure

How to integrate ActiveMQ in SpringBoot

Introduce maven dependencies

 <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.4.RELEASE</version>
    <relativepath></relativepath> 
  </parent>
  <properties>
    <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
    <project.reporting.outputencoding>UTF-8</project.reporting.outputencoding>
    <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-web</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </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>
      </plugin>
    </plugins>
  </build>
Copy after login

Introduce application .yml configuration

spring:
 activemq:
  broker-url: tcp://127.0.0.1:61616
  user: admin
  password: admin
queue: springboot-queue
server:
 port: 8080
Copy after login

Create QueueConfig

@Configuration
public class QueueConfig {
  @Value("${queue}")
  private String queue;

  @Bean
  public Queue logQueue() {
    return new ActiveMQQueue(queue);
  }

  @Bean
  public JmsTemplate jmsTemplate(ActiveMQConnectionFactory activeMQConnectionFactory, Queue queue) {
    JmsTemplate jmsTemplate = new JmsTemplate();
    jmsTemplate.setDeliveryMode(2);// 进行持久化配置 1表示非持久化,2表示持久化
    jmsTemplate.setConnectionFactory(activeMQConnectionFactory);
    jmsTemplate.setDefaultDestination(queue); // 此处可不设置默认,在发送消息时也可设置队列
    jmsTemplate.setSessionAcknowledgeMode(4);// 客户端签收模式
    return jmsTemplate;
  }

  // 定义一个消息监听器连接工厂,这里定义的是点对点模式的监听器连接工厂
  @Bean(name = "jmsQueueListener")
  public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(
      ActiveMQConnectionFactory activeMQConnectionFactory) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(activeMQConnectionFactory);
    // 设置连接数
    factory.setConcurrency("1-10");
    // 重连间隔时间
    factory.setRecoveryInterval(1000L);
    factory.setSessionAcknowledgeMode(4);
    return factory;
  }
}
Copy after login

Create producer:

@SpringBootApplication
@Component
@EnableScheduling
public class Producer {
  
  @Autowired
  private JmsMessagingTemplate jmsMessagingTemplate;
  
  @Autowired
  private Queue queue;
  
  @Scheduled(fixedDelay=3000)
  public void send() {
    String result = System.currentTimeMillis()+"---测试";
    System.out.println("result"+result);
    jmsMessagingTemplate.convertAndSend(queue,result);
  }
  public static void main(String[] args) {
    SpringApplication.run(Producer.class, args);
  }
}
Copy after login

Create consumer application.yml

spring:
 activemq:
  broker-url: tcp://127.0.0.1:61616
  user: admin
  password: admin
queue: springboot-queue
server:
 port: 8081
Copy after login

Create consumer:

@Component
@SpringBootApplication
public class consumer {

  private int count =0;
  
  @JmsListener(destination = "${queue}")
  public void receive(TextMessage textMessage,Session session) throws JMSException {
    String text = textMessage.getText();
    
    System.out.println("消费:"+text+"第几次获取消息count:"+(++count));
    
    System.out.println();
    String jmsMessageID = textMessage.getJMSMessageID();
  }
 
  public static void main(String[] args) {
    SpringApplication.run(consumer.class,args);
  }
}
Copy after login

The result is displayed:

How to integrate ActiveMQ in SpringBoot

The above is the detailed content of How to integrate ActiveMQ in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template