我已經(jīng)為這項任務(wù)苦苦掙扎了好幾天,所以我決定在這里問一個問題。好吧,我有一個遠(yuǎn)程 RabbitMQ 服務(wù)器,我只有發(fā)送和接收消息的權(quán)限。這意味著我不能創(chuàng)造任何東西。所以,我想接收和/或發(fā)送消息。但由于某種原因我不能這樣做。應(yīng)用程序啟動、通道創(chuàng)建、bean 初始化等。但是接收方和發(fā)送方什么都不做。我?guī)缀蹩梢钥隙ㄟ@個問題很簡單,我會認(rèn)為我在回答之后是個白癡,但現(xiàn)在我被卡住了。代碼如下。主類是標(biāo)準(zhǔn)的,沒有任何變化。配置類: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(); }}發(fā)件人: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 + "'"); }}應(yīng)用程序?qū)傩裕簊pring.rabbitmq.host=host.comspring.rabbitmq.port=5673spring.rabbitmq.username=usernamespring.rabbitmq.password=passwordspring.rabbitmq.ssl.enabled=true然后什么也沒有發(fā)生。問題是我做錯了什么,為什么它不能按照我想要的方式工作以及如何解決它?我知道復(fù)制粘貼整個項目并要求解決我的所有問題并不是一個很好的做法,對此我深表歉意,但目前我看不出有任何不同的方法可以使我的代碼正常工作。我很樂意得到任何幫助。
1 回答

天涯盡頭無女友
TA貢獻(xiàn)1831條經(jīng)驗 獲得超9個贊
為了能夠使用@Scheduled注釋,您必須首先使用 啟用調(diào)度@EnableScheduling。
嘗試將其添加到您的配置類:
@Configuration
@EnableScheduling
public class TestConfiguration { ... }
一旦你解決了這個問題,你可能還需要看看 wargre 的評論。
添加回答
舉報
0/150
提交
取消