为了账号安全,请及时绑定邮箱和手机立即绑定

Spring AMQP 项目不做任何事情

Spring AMQP 项目不做任何事情

慕妹3146593 2021-08-25 17:46:50
我已经为这项任务苦苦挣扎了好几天,所以我决定在这里问一个问题。好吧,我有一个远程 RabbitMQ 服务器,我只有发送和接收消息的权限。这意味着我不能创造任何东西。所以,我想接收和/或发送消息。但由于某种原因我不能这样做。应用程序启动、通道创建、bean 初始化等。但是接收方和发送方什么都不做。我几乎可以肯定这个问题很简单,我会认为我在回答之后是个白痴,但现在我被卡住了。代码如下。主类是标准的,没有任何变化。配置类:import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic 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.comspring.rabbitmq.port=5673spring.rabbitmq.username=usernamespring.rabbitmq.password=passwordspring.rabbitmq.ssl.enabled=true然后什么也没有发生。问题是我做错了什么,为什么它不能按照我想要的方式工作以及如何解决它?我知道复制粘贴整个项目并要求解决我的所有问题并不是一个很好的做法,对此我深表歉意,但目前我看不出有任何不同的方法可以使我的代码正常工作。我很乐意得到任何帮助。
查看完整描述

1 回答

?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

为了能够使用@Scheduled注释,您必须首先使用 启用调度@EnableScheduling。


尝试将其添加到您的配置类:


@Configuration

@EnableScheduling

public class TestConfiguration { ... }

一旦你解决了这个问题,你可能还需要看看 wargre 的评论。


查看完整回答
反对 回复 2021-08-25
  • 1 回答
  • 0 关注
  • 219 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信