2

standalone.xml (used to test through java main class)

<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");

Configured 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>    

**Code to initialize the channel - ** getOutboundMqtt is the method being invoked

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);
    }

Sending message to outbound MQTT - here is what I am facing issue

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";
    }

Error which is getting thrown @ 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 
  • 1
    Hello and welcome to StackOverflow. Please take some time to read the [help page](http://stackoverflow.com/help), especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the [Stack Overflow question checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – galath Jul 23 '15 at 07:44
  • So do I need to paste the complete code which I am executing? – Abhishek Agrawal Jul 23 '15 at 08:01
  • yes, please paste the code. – galath Jul 23 '15 at 08:03
  • 1
    You also need to show the complete stack trace, not just the exception message. – Gary Russell Jul 23 '15 at 13:20
  • Gary - I have updated the post. Please let me know whether you are looking for something else. – Abhishek Agrawal Jul 30 '15 at 14:47
  • 1
    The issue is solved. Was problem with the payload data type. My bad as this should be identified earlier. :( – Abhishek Agrawal Jul 31 '15 at 06:37
  • So, move the fix to the answer and accept it yourself! It is possible here! – Artem Bilan Aug 11 '15 at 19:21
  • @ArtemBilan - I didn't able to get your reply. Are you asking me to make the appropriate changes to the posted queries with working code? or just to post the fix which is being done to address problem? – Abhishek Agrawal Aug 20 '15 at 06:10

0 Answers0