如何根据spring boot getting started给rabbitmq发送消息
How to send mesage to rabitmq according spring boot getting started
我正在尝试按照 入门中的步骤操作
使用 RabbitMQ 发送消息
我已经下载并启动了 rabitmq。
我使用 windows 并作为服务启动:
我下载了项目框架并从文章中输入了代码。
因此项目结构如下所示:
申请:
package hello;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
public class Application {
final static String queueName = "spring-boot";
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("spring-boot-exchange");
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class);
}
}
接收方:
package hello;
import java.util.concurrent.CountDownLatch;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
private CountDownLatch latch = new CountDownLatch(1);
public void receiveMessage(String message) {
System.out.println("Received <" + message + ">");
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
}
亚军:
package hello;
import java.util.concurrent.TimeUnit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Runner implements CommandLineRunner {
private final RabbitTemplate rabbitTemplate;
private final Receiver receiver;
private final ConfigurableApplicationContext context;
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
ConfigurableApplicationContext context) {
this.receiver = receiver;
this.rabbitTemplate = rabbitTemplate;
this.context = context;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Sending message...");
rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
context.close();
}
}
然后在指南中写到我需要启动 main 方法,我会看到这样的输出:
Sending message...
Received
但是我没有看到这些行。
我只看到像这样的行:
15:51:45.720 [container-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Retrieving delivery for Consumer@5bb8f9e2: tags=[{amq.ctag-2wLW2IpZdDrHrPqtlqOd4Q=spring-boot}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3921095e Shared Rabbit Connection: SimpleConnection@226c7c81 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 53157], acknowledgeMode=AUTO local queue size=0
15:51:46.721 [container-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Retrieving delivery for Consumer@5bb8f9e2: tags=[{amq.ctag-2wLW2IpZdDrHrPqtlqOd4Q=spring-boot}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3921095e Shared Rabbit Connection: SimpleConnection@226c7c81 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 53157], acknowledgeMode=AUTO local queue size=0
15:51:47.722 [container-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Retrieving delivery for Consumer@5bb8f9e2: tags=[{amq.ctag-2wLW2IpZdDrHrPqtlqOd4Q=spring-boot}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3921095e Shared Rabbit Connection: SimpleConnection@226c7c81 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 53157], acknowledgeMode=AUTO local queue size=0
P.S.
我知道 Runner
class 没有在我的应用程序中使用,但我不明白如何实例化它以及在何处调用。我在指南中找不到相关信息。
请帮助我开始申请。
您的问题在:
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class);
}
我们在这里讨论 Spring 引导,因此该代码必须是:
SpringAplication.run(Application.class, args);
我正在尝试按照 入门中的步骤操作 使用 RabbitMQ 发送消息
我已经下载并启动了 rabitmq。
我使用 windows 并作为服务启动:
我下载了项目框架并从文章中输入了代码。
因此项目结构如下所示:
申请:
package hello;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
public class Application {
final static String queueName = "spring-boot";
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("spring-boot-exchange");
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class);
}
}
接收方:
package hello;
import java.util.concurrent.CountDownLatch;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
private CountDownLatch latch = new CountDownLatch(1);
public void receiveMessage(String message) {
System.out.println("Received <" + message + ">");
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
}
亚军:
package hello;
import java.util.concurrent.TimeUnit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Runner implements CommandLineRunner {
private final RabbitTemplate rabbitTemplate;
private final Receiver receiver;
private final ConfigurableApplicationContext context;
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
ConfigurableApplicationContext context) {
this.receiver = receiver;
this.rabbitTemplate = rabbitTemplate;
this.context = context;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Sending message...");
rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
context.close();
}
}
然后在指南中写到我需要启动 main 方法,我会看到这样的输出:
Sending message...
Received
但是我没有看到这些行。
我只看到像这样的行:
15:51:45.720 [container-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Retrieving delivery for Consumer@5bb8f9e2: tags=[{amq.ctag-2wLW2IpZdDrHrPqtlqOd4Q=spring-boot}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3921095e Shared Rabbit Connection: SimpleConnection@226c7c81 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 53157], acknowledgeMode=AUTO local queue size=0
15:51:46.721 [container-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Retrieving delivery for Consumer@5bb8f9e2: tags=[{amq.ctag-2wLW2IpZdDrHrPqtlqOd4Q=spring-boot}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3921095e Shared Rabbit Connection: SimpleConnection@226c7c81 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 53157], acknowledgeMode=AUTO local queue size=0
15:51:47.722 [container-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Retrieving delivery for Consumer@5bb8f9e2: tags=[{amq.ctag-2wLW2IpZdDrHrPqtlqOd4Q=spring-boot}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3921095e Shared Rabbit Connection: SimpleConnection@226c7c81 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 53157], acknowledgeMode=AUTO local queue size=0
P.S.
我知道 Runner
class 没有在我的应用程序中使用,但我不明白如何实例化它以及在何处调用。我在指南中找不到相关信息。
请帮助我开始申请。
您的问题在:
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class);
}
我们在这里讨论 Spring 引导,因此该代码必须是:
SpringAplication.run(Application.class, args);