因此,我已经开始使用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())
}
}
}
使用启用了mqtt的official docker rabbitmq图像。
我需要纠正什么才能完成这项工作?
最佳答案
MQTT插件以mqtt主题名称作为路由键发布到amq.topic
。
在使用者方面,它将带有路由键的自动删除队列绑定到该交换机。在下面的示例中,该队列名为mqtt-subscription-mqttConsumerqos1
。
为了通过AMQP接收MQTT消息,您需要将自己的队列绑定到交换。这是一个例子:
@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 - 我如何通过MQTT进行传输以及如何使用RabbitMQ和Spring-AMQP在AMQP上进行接收,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54995261/