<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
# RABBIT (RabbitProperties)
#spring.rabbitmq.host=localhost
#spring.rabbitmq.port=5672
#spring.rabbitmq.password=
#spring.rabbitmq.username=
package com.shuju.test2.util.configuration;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* amqp 队列配置
*/
@Configuration
public class AmqpConfiguration {
@Bean
public Queue queue() {
return new Queue("demodemo.queue"); //创业队列
}
}
package com.shuju.test2.component;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class DemoAmqpComponent {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(String msg) {
this.amqpTemplate.convertAndSend("demodemo.queue", msg);
}
@RabbitListener(queues = "demodemo.queue")
public void receiveQueue(String text) {
System.out.println("接受到:" + text);
}
}
package com.shuju.test2;
import com.shuju.test2.component.DemoAmqpComponent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test2ApplicationTests {
@Autowired
private DemoAmqpComponent demoAmqpComponent;
@Test
public void send() {
demoAmqpComponent.send("hello world2");
}
}
sudo docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 -v `pwd`/data:/work/docker/data/rabbitmq -e RABBITMQ_DEFAULT_VHOST=my_vhost -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin rabbitmq:3-management