standalone.xml(用于测试java主类)
<bean id="clientFactory"
class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<!-- This is channel declaration -->
<int:channel id="mqttOutChannel"/>
<!-- This will publish the message to the specific topic -->
<int-mqtt:outbound-channel-adapter id="mqOutAdpter"
client-id="client1"
url="tcp://localhost:1883"
client-factory="clientFactory"
default-topic="testPublishMqttServer"
channel="mqttOutChannel"
auto-startup="true"
converter="converter"/>
<bean id="converter" class="org.springframework.integration.mqtt.support.DefaultPahoMessageConverter">
</bean>
<int:inbound-channel-adapter channel="mqttOutChannel" expression="'Hi'">
<int:poller fixed-delay="1000"></int:poller>
</int:inbound-channel-adapter>
Java Main Class
ApplicationContext ac = new ClassPathXmlApplicationContext("/standalone.xml.xml");
MessageChannel msg = (MessageChannel)ac.getBean("mqttOutChannel");
msg.send(MessageBuilder.withPayload("My Msg").setHeader("HeaderVar", "Hello").build());
System.out.println("Its Working");
已配置的XML - mqtt-outbound-context
<bean id="clientFactory"
class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
<property name="userName" value="${mqtt.outbound.userName}"/>
<property name="password" value="${mqtt.outbound.passWord}"/>
</bean>
<!-- This is channel declaration -->
<int:channel id="mqttOutChannel"/>
<!-- This will publish the message to the specific topic -->
<int-mqtt:outbound-channel-adapter id="${mqtt.outbound.beanId}"
client-id="${mqtt.outbound.clientId}"
url="${mqtt.outbound.brokerUrl}"
client-factory="clientFactory"
default-topic="${mqtt.outbound.topic}"
channel="mqttOutChannel"
auto-startup="true"
converter="converter"/>
<bean id="converter" class="org.springframework.integration.mqtt.support.DefaultPahoMessageConverter">
</bean>
**初始化频道的代码 - ** getOutboundMqtt是被调用的方法
public MessageChannel getOutboundMqtt(String endPoint,String brokerUrl,String topics,String userName,
String passWord,String sourceId) {
MessageChannel channel = myChannelDetails.getChannel("MQ_OUTBOUND", endPoint);
if (channel == null) {
channel = createNewChannel(endPoint, brokerUrl, topics, userName, passWord,sourceId);
}
return channel;
}
private synchronized MessageChannel createNewChannel(String endPoint,String brokerUrl,String topics,String userName,
String passWord,String sourceId) {
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { "/META-INF/spring/integration/mqtt/mqtt-outbound-context.xml" },
false);
this.setEnvironmentForMqtt(ctx, endPoint, brokerUrl, topics, userName, passWord);
ctx.refresh();
MessageChannel channel = ctx.getBean("mqttOutChannel", MessageChannel.class);
myChannelDetails.putChannel("MQ_OUTBOUND", endPoint, channel);
return channel;
}
private void setEnvironmentForMqtt(ConfigurableApplicationContext ctx, String endPoint,String brokerUrl,String topics,String userName,
String passWord) {
StandardEnvironment env = new StandardEnvironment();
Properties props = new Properties();
props.setProperty("mqtt.outbound.brokerUrl", brokerUrl);
props.setProperty("mqtt.outbound.topic", topics);
props.setProperty("mqtt.outbound.userName", userName);
props.setProperty("mqtt.outbound.passWord", passWord);
props.setProperty("mqtt.outbound.clientId", endPoint);
props.setProperty("mqtt.outbound.beanId", endPoint+"ID");
PropertiesPropertySource pps = new PropertiesPropertySource("mqttProps", props);
env.getPropertySources().addLast(pps);
ctx.setEnvironment(env);
}
向出站MQTT发送消息 - 这是我面临的问题
public String putMessageToChannel( String mqttChannelName,String endPoint, String msg)
{
myChannelDetails.getChannel(mqttChannelName,
endPoint).send(MessageBuilder.withPayload(msg).setHeader("HeaderVar", "Hello").build());
return "Successfully sent the message to the recipient";
}
被抛出的错误@ putMessageToChannel
org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler#0]; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this expression must be true