簡體   English   中英

Spring Integration手動將消息發布到渠道

[英]Spring Integration manually publish message to channel

我正在學習如何使用Java Spring框架,並開始嘗試使用Spring Integration。 我正在嘗試使用Spring Integration將我的應用程序連接到MQTT代理,以發布和訂閱消息,但是我無法找到一種手動將消息發布到出站通道的方法。 如果可能的話,我想專門使用Java代碼中的符號而不是定義bean和其他相關配置的xml文件來構建它。

在每個示例中,我都看到手動發布消息的解決方案似乎是使用MessagingGateway接口,然后使用SpringApplicationBuilder獲取ConfigurableApplicationContext來獲取main方法中對網關接口的引用。 然后,該引用用於發布消息。 可以將AutoWired用作該接口嗎? 在我的嘗試中,我只是得到了一個N​​ullPointer。

我的目標是構建一個我訂閱主題以獲取游戲消息的游戲,然后每當用戶准備進行下一步操作時,就向該主題發布新消息。

更新 :這是我一直在查看的有關設置出站渠道的示例之一: https : //docs.spring.io/spring-integration/reference/html/mqtt.html

在Gary Russel回答后更新2:

這是我查看示例后編寫的一些示例代碼,當在Controller.java中運行gateway.sendToMqtt時,使用@AutoWired作為網關時會得到NullPointer。 我要在這里實現的是在控制器處理GET請求時手動發送mqtt消息。

Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

Controller.java

@RestController
@RequestMapping("/publishMessage")
public class Controller {

    @Autowired
    static Gateway gateway;

    @RequestMapping(method = RequestMethod.GET)
    public int request(){
        gateway.sendToMqtt("Test Message!");
        return 0;
    }
}

MqttPublisher.java

@EnableIntegration
@Configuration
public class MqttPublisher {
    @Bean
    public MqttPahoClientFactory mqttClientFactory(){
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        factory.setServerURIs("tcp://localhost:1883");
        return factory;
    }

    @Bean
    @ServiceActivator(inputChannel = "mqttOutboundChannel")
    public MessageHandler mqttOutbound(){
        MqttPahoMessageHandler messageHandler =
                new MqttPahoMessageHandler("clientPublisher", mqttClientFactory());
        messageHandler.setAsync(true);
        messageHandler.setDefaultTopic("topic");
        return messageHandler;
    }

    @Bean
    public MessageChannel mqttOutboundChannel(){
        return new DirectChannel();
    }

    @MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
    public interface Gateway {

        void sendToMqtt(String data);
    }
}

更新

不知道這是否是正確的日志記錄,但這是我添加后得到的結果:

logging.level.org.springframework.web=Debug
logging.level.org.hibernate=Error

到application.properties。

https://hastebin.com/cuvonufeco.hs

使用消息傳遞網關或僅將消息發送到通道。

編輯

@SpringBootApplication
public class So47846492Application {

    public static void main(String[] args) {
        SpringApplication.run(So47846492Application.class, args).close();
    }

    @Bean
    public ApplicationRunner runner(MyGate gate) {
        return args -> {
            gate.send("someTopic", "foo");
            Thread.sleep(5_000);
        };
    }

    @Bean
    @ServiceActivator(inputChannel = "toMqtt")
    public MqttPahoMessageHandler mqtt() {
        MqttPahoMessageHandler handler = new MqttPahoMessageHandler("tcp://localhost:1883", "foo",
                clientFactory());
        handler.setDefaultTopic("myTopic");
        handler.setQosExpressionString("1");
        return handler;
    }

    @Bean
    public MqttPahoClientFactory clientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        factory.setUserName("guest");
        factory.setPassword("guest");
        return factory;
    }

    @Bean
    public MqttPahoMessageDrivenChannelAdapter mqttIn() {
        MqttPahoMessageDrivenChannelAdapter adapter =
                new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "bar", "someTopic");
        adapter.setOutputChannelName("fromMqtt");
        return adapter;
    }

    @ServiceActivator(inputChannel = "fromMqtt")
    public void in(String in) {
        System.out.println(in);
    }

    @MessagingGateway(defaultRequestChannel = "toMqtt")
    public interface MyGate {

        void send(@Header(MqttHeaders.TOPIC) String topic, String out);

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM