你好,大家好,
我正在用Spring/Docker构建一个简单的RabbitMQ AMQP服务器来运行服务器本身,我遵循以下指南:www.example.comhttps://spring.io/guides/gs/messaging-rabbitmq/#scratch
这些类实际上是从那里复制过来的。
我在src/main/java/com. example. messagingrabbitmq中有3个类,它们是:
消息传递Rabbitmq应用程序:
package com.example.messagingrabbitmq;
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.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MessagingRabbitmqApplication {
static final String topicExchangeName = "spring-boot-exchange";
static final String queueName = "spring-boot";
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange(topicExchangeName);
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#");
}
@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 {
SpringApplication.run(MessagingRabbitmqApplication.class, args).close();
}
}
接收者:
package com.example.messagingrabbitmq;
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 com.example.messagingrabbitmq;
import java.util.concurrent.TimeUnit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class Runner implements CommandLineRunner {
private final RabbitTemplate rabbitTemplate;
private final Receiver receiver;
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) {
this.receiver = receiver;
this.rabbitTemplate = rabbitTemplate;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Sending message...");
rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!");
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
}
}
我尝试从MessagingRabbitmqApplication java类运行该程序,但导致以下错误:
Description:
Parameter 0 of method listenerAdapter in com.example.messagingrabbitmq.MessagingRabbitmqApplication required a bean of type 'com.example.messagingrabbitmq.Receiver' that could not be found.
Action:
Consider defining a bean of type 'com.example.messagingrabbitmq.Receiver' in your configuration.
Process finished with exit code 1
我尝试在www.example.com类中创建一个Receiver Bean,但它导致了相同的错误:Receiver.java class with the following, but it resulted in the same error:
@Bean
Receiver receiver() {
return new Receiver();
}
我还有一个关于如何设置我的www.example.com文件以便可以在我正在运行的Docker容器中设置队列/交换的问题?它当前正在"localhost:15672"上运行。application.properties file so the Queues/Exchanges can be set up in my running Docker container? It is currently running on 'localhost:15672'.
非常感谢您事先的帮助。
1条答案
按热度按时间bwntbbo31#
我检查了你现有的代码,它工作正常。
测试放大器应用:
接收者:
跑手:
控制台输出的屏幕截图:
使用maven正确编译您的项目。再次执行maven
clean
和install
阶段。如果您仍然面临问题,请执行以下步骤:
中项目的缓存无效