spring boot ActiveMQ

1.spring-boot-starter-parent 1.3.1.RELEASE 版本

1.1消费者

1.1.1pom.xml配置


    org.springframework.boot
    spring-boot-starter



    org.springframework
    spring-jms



    org.apache.activemq
    activemq-client



    org.apache.activemq
    activemq-broker

1.1.2application.properties

spring.activemq.broker-url=tcp://localhost:61616

1.1.3Receiver.java

@Component
public class Receiver {

    @JmsListener(destination="my-destination")
    public void receiveMessage(String message){
        System.out.println("接收到:<"+message+">");
    }
}

1.1.4App.java

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

1.1.5启动

运行App.java的main方法即可

1.2生产者

1.2.1pom.xml


    org.springframework.boot
    spring-boot-starter



    org.springframework
    spring-jms



    org.apache.activemq
    activemq-client



    org.apache.activemq
    activemq-broker

1.2.2application.properties

spring.activemq.broker-url=tcp://localhost:61616

1.2.3Msg.java

package com.shuai.spring_boot_nineteen;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.MessageCreator;

public class Msg implements MessageCreator {

    @Override
    public Message createMessage(Session session) throws JMSException {
        return session.createTextMessage("测试消息");
    }
}

1.2.4App.java

package com.shuai.spring_boot_nineteen;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.core.JmsTemplate;

@SpringBootApplication
public class App implements CommandLineRunner{
    
    @Autowired
    JmsTemplate jmsTemplate;
    
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        jmsTemplate.send("my-destination",new Msg());
    }
}

1.2.5运行

运行App.java中的main方法

你可能感兴趣的:(spring boot ActiveMQ)