gpt4 book ai didi

java - 如何使用 RabbitMQ 和 Spring-AMQP 通过 MQTT 传输并在 AMQP 上接收

转载 作者:行者123 更新时间:2023-12-02 01:37:36 27 4
gpt4 key购买 nike

所以我已经让 MQTT -> MQTT 和 AMQP -> AMQP 工作;不过,MQTT -> AMQP 的翻译似乎在某处不起作用。这是我的测试,如果我的“监听器”也在使用 paho 的 MQTT 中,则它会通过,但这个rabbitmq 实现不会。

@SpringBootTest
@SpringJUnitConfig
internal open class ProvisioningTest @Autowired constructor(
private val mqtt: IMqttAsyncClient,
private val mapper: ObjectMapper
) {

@Test
fun provision() {
val entity = Foley(
rfid = UUID.randomUUID().toString(),
)

val called = AtomicBoolean(false)
mqtt.subscribe("foley/created", 1) { _, _ -> called.set(true) }

mqtt.publish("foley/new", MqttMessage(mapper.writeValueAsBytes(entity)))

Awaitility.await().atMost(10, TimeUnit.SECONDS).untilTrue(called)
}
}

这是将保存的实体发布到其他队列的监听器;当我发布到 MQTT 时,它永远不会被调用。

@Service
open class Provisioning(private val repo: FoleyRepo) {
private val log: Logger = LogManager.getLogger(this::class.java)

@SendTo("foley.created")
@RabbitListener(queuesToDeclare = [Queue("foley.new")] )
open fun listen(entity: Foley): Foley {
log.trace("saving: {}", entity)
val save = repo.save(entity)
log.debug("saved: {}", save)
return save
}

}

我的整个消息配置

@Configuration
open class MessagingConfig {

@Bean
open fun client(
@Value("tcp://\${mqtt.client.host:localhost}:\${mqtt.client.port:1883}") uri: String,
@Value("\${mqtt.client.user:#{null}}") user: String?,
@Value("\${mqtt.client.pass:#{null}}") pass: CharArray?
): IMqttAsyncClient {

val connOpt = MqttConnectOptions()
user?.let { connOpt.userName = it }
pass?.let { connOpt.password = it }
connOpt.isCleanSession = false
connOpt.isAutomaticReconnect = true
val client = MqttAsyncClient(uri, MqttAsyncClient.generateClientId(), MemoryPersistence())
client.connect(connOpt)
return client
}

@Bean
open fun messageConverter( om: ObjectMapper): MessageConverter {
return Jackson2JsonMessageConverter(om)
}

@Bean
open fun builder(): Jackson2ObjectMapperBuilderCustomizer {
return Jackson2ObjectMapperBuilderCustomizer {
it.modules(JavaTimeModule(), KotlinModule())
}
}
}

使用official docker rabbitmq启用了 mqtt 的图像。

我需要纠正什么才能使其正常工作?

最佳答案

MQTT 插件以 mqtt 主题名称作为路由键发布到 amq.topic

在消费者方面,它使用路由键将自动删除队列绑定(bind)到该交换器;在以下示例中,队列名为 mqtt-subscription-mqttConsumerqos1

为了通过 AMQP 接收 MQTT 消息,您需要将自己的队列绑定(bind)到交换器。这是一个例子:

@SpringBootApplication
public class So54995261Application {

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

@Bean
@ServiceActivator(inputChannel = "toMQTT")
public MqttPahoMessageHandler sendIt(MqttPahoClientFactory clientFactory) {
MqttPahoMessageHandler handler = new MqttPahoMessageHandler("clientId", clientFactory);
handler.setAsync(true);
handler.setDefaultTopic("so54995261");
return handler;
}

@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
MqttConnectOptions options = new MqttConnectOptions();
options.setServerURIs(new String[] { "tcp://localhost:1883" });
options.setUserName("guest");
options.setPassword("guest".toCharArray());
factory.setConnectionOptions(options);
return factory;
}

@Bean
public MessageProducerSupport mqttInbound() {
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("mqttConsumer",
mqttClientFactory(), "so54995261");
adapter.setCompletionTimeout(5000);
return adapter;
}

@Bean
public IntegrationFlow flow() {
return IntegrationFlows.from(mqttInbound())
.handle(System.out::println)
.get();
}

@RabbitListener(queues = "so54995261")
public void listen(byte[] in) {
System.out.println(new String(in));
}

@Bean
public Queue queue() {
return new Queue("so54995261");
}

@Bean
public Binding binding() {
return new Binding("so54995261", DestinationType.QUEUE, "amq.topic", "so54995261", null);
}

@Bean
public ApplicationRunner runner(MessageChannel toMQTT) {
return args -> toMQTT.send(new GenericMessage<>("foo"));
}

}

关于java - 如何使用 RabbitMQ 和 Spring-AMQP 通过 MQTT 传输并在 AMQP 上接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54995261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com