spring integration-mqtt型转换器问题

时间:2015-01-08 11:33:37

标签: spring spring-integration

尝试从web运行示例spring-integration mqtt项目。 我在根上下文中导入了mqtt-context。部署战争后,我正在运行RunMqtt.java文件。但是得到以下问题。如果在独立模式下运行,则同一文件不会产生任何问题。

堆栈跟踪 创建bean的实例' startCaseAdapter' 16:50:54.147 DEBUG [main] [org.springframework.beans.BeanUtils]根据类型org.springframework.integration.mqtt.core.MqttPahoClientFactory找不到属性编辑器[org.springframework.integration.mqtt.core.MqttPahoClientFactoryEditor] '编辑'后缀约定 16:50:54.148 TRACE [main] [org.springframework.beans.TypeConverterDelegate]字段[topic]不是枚举值 java.lang.NoSuchFieldException:topic     在java.lang.Class.getField(Class.java:1579)     at org.springframework.beans.TypeConverterDelegate.attemptToConvertStringToEnum(TypeConverterDelegate.java:336)     在org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:257)     在org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)     在org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)     在org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47)     在org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:706)     在org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1133)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1036)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)     在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)     在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)     在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)     org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)     在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)     在com.iux.ieg.test.mqtt.RunMqtt.test(RunMqtt.java:24)     在com.iux.ieg.test.mqtt.RunMqtt.main(RunMqtt.java:46) 16:50:54.150 TRACE [main] [org.springframework.beans.factory.support.DefaultListableBeanFactory]忽略构造函数[public org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter(java.lang.String,java.lang.String, bean的“org.springframework.integration.mqtt.core.MqttPahoClientFactory,java.lang.String [])]' startCaseAdapter':org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为&#39的bean时出错; startCaseAdapter':通过构造函数参数表达的不满意的依赖关系,类型为[org.springframework.integration.mqtt.core.MqttPahoClientFactory]的索引2:无法将类型为[java.lang.String]的构造函数参数值转换为必需的类型[ org.springframework.integration.mqtt.core.MqttPahoClientFactory]:无法转换类型' java.lang.String'的值要求的类型' org.springframework.integration.mqtt.core.MqttPahoClientFactory&#39 ;;嵌套异常是java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为必需的类型[org.springframework.integration.mqtt.core.MqttPahoClientFactory]:找不到匹配的编辑器或转换策略 16:50:54.150 TRACE [main] [org.springframework.beans.TypeConverterDelegate]使用属性编辑器将[String]转换为[class [Ljava.lang.String;] [org.springframework.beans.propertyeditors.StringArrayPropertyEditor@821075] 16:50:54.150 TRACE [main] [org.springframework.beans.TypeConverterDelegate]字段[clientId]不是枚举值 java.lang.NoSuchFieldException:clientId     在java.lang.Class.getField(Class.java:1579)     at org.springframework.beans.TypeConverterDelegate.attemptToConvertStringToEnum(TypeConverterDelegate.java:336)     在org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:257)     在org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)     在org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)     在org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47)     在org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:706)     在org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1133)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1036)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)     在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)     在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)     在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)     在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)     在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)     在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)     在com.iux.ieg.test.mqtt.RunMqtt.test(RunMqtt.java:24)     在com.iux.ieg.test.mqtt.RunMqtt.main(RunMqtt.java:46) 配置 网络的应用程序-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&#34;&GT;

<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 个答案:

答案 0 :(得分:1)

  

示例spring-integration mqtt项目

哪个样本?

请显示您的配置。

修改

看起来Spring无法确定要使用哪个构造函数。

尝试添加

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

并将client-factory="clientFactory"添加到邮件驱动的适配器。