0
votes

Trying to run the sample spring-integration mqtt project from web. I have imported the mqtt-context in root context. After the war is deployed I am running the RunMqtt.java file. But getting the following issue. If running in standalone mode , the same file does not give any issue.

Stack Trace Creating instance of bean 'startCaseAdapter' 16:50:54.147 DEBUG [main][org.springframework.beans.BeanUtils] No property editor [org.springframework.integration.mqtt.core.MqttPahoClientFactoryEditor] found for type org.springframework.integration.mqtt.core.MqttPahoClientFactory according to 'Editor' suffix convention 16:50:54.148 TRACE [main][org.springframework.beans.TypeConverterDelegate] Field [topic] isn't an enum value java.lang.NoSuchFieldException: topic at java.lang.Class.getField(Class.java:1579) at org.springframework.beans.TypeConverterDelegate.attemptToConvertStringToEnum(TypeConverterDelegate.java:336) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:257) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107) at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64) at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:706) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1133) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1036) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at com.iux.ieg.test.mqtt.RunMqtt.test(RunMqtt.java:24) at com.iux.ieg.test.mqtt.RunMqtt.main(RunMqtt.java:46) 16:50:54.150 TRACE [main][org.springframework.beans.factory.support.DefaultListableBeanFactory] Ignoring constructor [public org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter(java.lang.String,java.lang.String,org.springframework.integration.mqtt.core.MqttPahoClientFactory,java.lang.String[])] of bean 'startCaseAdapter': org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'startCaseAdapter': Unsatisfied dependency expressed through constructor argument with index 2 of type [org.springframework.integration.mqtt.core.MqttPahoClientFactory]: Could not convert constructor argument value of type [java.lang.String] to required type [org.springframework.integration.mqtt.core.MqttPahoClientFactory]: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.integration.mqtt.core.MqttPahoClientFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.integration.mqtt.core.MqttPahoClientFactory]: no matching editors or conversion strategy found 16:50:54.150 TRACE [main][org.springframework.beans.TypeConverterDelegate] Converting String to [class [Ljava.lang.String;] using property editor [org.springframework.beans.propertyeditors.StringArrayPropertyEditor@821075] 16:50:54.150 TRACE [main][org.springframework.beans.TypeConverterDelegate] Field [clientId] isn't an enum value java.lang.NoSuchFieldException: clientId at java.lang.Class.getField(Class.java:1579) at org.springframework.beans.TypeConverterDelegate.attemptToConvertStringToEnum(TypeConverterDelegate.java:336) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:257) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107) at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64) at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:706) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1133) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1036) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at com.iux.ieg.test.mqtt.RunMqtt.test(RunMqtt.java:24) at com.iux.ieg.test.mqtt.RunMqtt.main(RunMqtt.java:46) Configuration Web-application-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- This file will be the root context file for web app. All other context 
        will be imported here -->
    <!-- Security context . Now the spring security with basic authentication 
        implemented. OAuth2 will be implemented -->
    <import resource="security-config.xml" />
    <!-- rest service call context -->
    <import
        resource="classpath:META-INF/spring/integration/rest/applicationContext-http-int.xml" />
    <!-- Sftp context -->
    <import
        resource="classpath:META-INF/spring/integration/sftp/SftpInboundReceive-context.xml" />
    <import
        resource="classpath:META-INF/spring/integration/sftp/SftpOutboundTransfer-poll.xml" />
    <!-- mqtt context -->
    <import resource="classpath:META-INF/spring/integration/mqtt/mqtt-context.xml" />
    <!-- Mail Context -->
    <import
        resource="classpath:META-INF/spring/integration/mail/mail-imap-idle-config.xml" />
    <import
        resource="classpath:META-INF/spring/integration/mail/mail-pop3-config.xml" />


    <!--Component scan base package -->
    <context:component-scan base-package="com.iux.ieg" />
    <!-- All the property configuration moved to parent context file to solve 
        the propert not found exception -->
    <!-- <context:property-placeholder order="1" location="classpath:/sftpuser.properties, 
        classpath:/sftpfile.properties,classpath:/resthttp.properties" ignore-unresolvable="true"/> -->
    <context:property-placeholder order="0"
        location="classpath:/sftpfile.properties" ignore-unresolvable="true" />
    <context:property-placeholder order="1"
        location="classpath:/sftpuser.properties" ignore-unresolvable="true" />
    <context:property-placeholder order="2"
        location="classpath:/resthttp.properties" ignore-unresolvable="true" />
    <context:property-placeholder order="3"
        location="classpath:/mqtt.properties" ignore-unresolvable="true" />
    <context:property-placeholder order="4"
        location="classpath:/mail.properties" />

</beans>

mqtt-context.xml

http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/integration/mqtt http://www.springframework.org/schema/integration/mqtt/spring-integration-mqtt-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<context:property-placeholder 
    location="classpath:/mqtt.properties" ignore-unresolvable="true" />


<!-- intercept and log every message -->
<int:logging-channel-adapter id="logger"
    level="ERROR" />
<int:wire-tap channel="logger" />
<!-- Mark the auto-startup="true" for starting MqttPahoMessageDrivenChannelAdapter from configuration  -->
<int-mqtt:message-driven-channel-adapter
    id="startCaseAdapter" client-id="clientId" url="${mqtt.brokerurl}"
    topics="topic" channel="startCase" auto-startup="true" />
<int:channel id="startCase" />


<int:service-activator id="startCaseService"
    input-channel="startCase" ref="mqttCaseService" method="startCase" />

<bean id="mqttCaseService" class="com.iux.ieg.mqtt.MqttCaseService" />

MqttCaseService.java

import org.apache.log4j.Logger;


public class MqttCaseService {
    private static Logger logger = Logger.getLogger(MqttCaseService.class);

    public void startCase(String message){
        logger.debug(message);
    }

}

RunMqtt.java

public class RunMqtt {
    private static Logger logger = Logger.getLogger(RunMqtt.class);
    //@Test
    public void test() throws MqttException{
        ConfigurableApplicationContext context =
                new ClassPathXmlApplicationContext("/META-INF/spring/integration/mqtt/mqtt-context.xml");
        logger.debug(context);
        //MqttPahoMessageDrivenChannelAdapter startCaseAdapter = (MqttPahoMessageDrivenChannelAdapter)context.getBean("startCaseAdapter");
        //Uncomment to stop the adapter manually from program
        //startCaseAdapter.start();
        //DefaultMqttPahoClientFactory mqttClient = (DefaultMqttPahoClientFactory)ac.getBean("clientFactory");
        DefaultMqttPahoClientFactory mqttClient = new DefaultMqttPahoClientFactory();

        MqttClient mclient = mqttClient.getClientInstance("tcp://*messagebrokerurl*:1883", "JavaSample");
        String data = "This is what I am sending in 2nd attempt";
        MqttMessage mm = new MqttMessage(data.getBytes());
        mm.setQos(1);
        mclient.connect();
        mclient.publish("topic",mm);
        mclient.disconnect();
        //Uncomment to stop the adapter manually from program
        //startCaseAdapter.stop();
    }


    public static void main(String[] args)  {
        try {
            new RunMqtt().test();
        } catch (MqttException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
1

1 Answers

1
votes

the sample spring-integration mqtt project

Which sample?

Please show your configuration.

EDIT:

It looks like Spring is having trouble determining which constructor to use.

Try adding

<bean id="clientFactory" class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory" />

and add client-factory="clientFactory" to the message-driven adapter.