spring 集成-mqtt 类型转换器问题

spring integration-mqtt type converter issue

正在尝试从 Web 运行示例 spring-integration mqtt 项目。 我已经在根上下文中导入了 mqtt-context。 war 部署后,我正在运行 RunMqtt.java 文件。但是遇到以下问题。如果以独立模式运行,相同的文件不会出现任何问题。

堆栈跟踪 创建 bean 实例 'startCaseAdapter' 16:50:54.147 DEBUG [main][org.springframework.beans.BeanUtils] 根据 'Editor' 后缀约定,未找到类型 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: 话题 在 java.lang.Class.getField(Class.java:1579) 在 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) 在 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,org.springframework.integration.mqtt.core.MqttPahoClientFactory,[=103= bean 'startCaseAdapter' 的][])]:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'startCaseAdapter' 的 bean 时出错:通过具有类型 [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';嵌套异常是 java.lang.IllegalStateException:无法将类型 [java.lang.String] 的值转换为所需类型 [org.springframework.integration.mqtt.core.MqttPahoClientFactory]:未找到匹配的编辑器或转换策略 16:50:54.150 TRACE [main][org.springframework.beans.TypeConverterDelegate] 使用属性编辑器将字符串转换为 [class [Ljava.lang.String;] [org.springframework.beans.propertyeditors.StringArrayPropertyEditor@821075] 16:50:54.150 TRACE [main][org.springframework.beans.TypeConverterDelegate] 字段 [clientId] 不是枚举值 java.lang.NoSuchFieldException: 客户编号 在 java.lang.Class.getField(Class.java:1579) 在 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) 在 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">

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

    }

the sample spring-integration mqtt project

哪个样本?

请出示您的配置。

编辑:

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

尝试添加

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

并将client-factory="clientFactory"添加到消息驱动适配器。