我已经为这项任务苦苦挣扎了好几天,所以我决定在这里问一个问题。
好吧,我有一个远程 RabbitMQ 服务器,我只有发送和接收消息的权限。这意味着我不能创造任何东西。
所以,我想接收和/或发送消息。但由于某种原因我不能这样做。应用程序启动、通道创建、bean 初始化等。但是接收方和发送方什么都不做。
我几乎可以肯定这个问题很简单,我会认为我在回答之后是个白痴,但现在我被卡住了。
代码如下。主类是标准的,没有任何变化。
配置类:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration {
@Bean
public Sender sender() {
return new Sender();
}
@Bean
public Receiver receiver() {
return new Receiver();
}
}
发件人:
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
public class Sender {
@Autowired
private RabbitTemplate template;
@Scheduled(fixedDelay = 1000, initialDelay = 500)
public void send() {
String message = "Hello World!";
this.template.convertAndSend("ExchangeName","RoutingKey", message);
System.out.println(" [x] Sent '" + message + "'");
}
}
接收者:
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
@RabbitListener(queues = "QueueName")
public class Receiver {
@RabbitHandler
public void receive(String in) {
System.out.println(" [x] Received '" + in + "'");
}
}
应用程序属性:
spring.rabbitmq.host=host.com
spring.rabbitmq.port=5673
spring.rabbitmq.username=username
spring.rabbitmq.password=password
spring.rabbitmq.ssl.enabled=true
然后什么也没有发生。问题是我做错了什么,为什么它不能按照我想要的方式工作以及如何解决它?
我知道复制粘贴整个项目并要求解决我的所有问题并不是一个很好的做法,对此我深表歉意,但目前我看不出有任何不同的方法可以使我的代码正常工作。我很乐意得到任何帮助。
天涯尽头无女友
相关分类