Camel MQTT配置选项DSL路由URL参数,bean值不起作用

时间:2017-08-08 23:33:41

标签: spring-boot routes apache-camel mqtt publish-subscribe

我试图从外部经纪人(AWs IoT)测试pub / sub;从camel-example-spring-boot示例项目开始,添加了thecamel-mqtt-starter。在我尝试定义mqtt路由之前,一切似乎都能正常工作。我在配置sslContext url参数时遇到问题:

@Configuration
public class AppConfig {


@Bean(name="awsiotsslcontext")
SSLContext awsiotsslcontext(){

    SSLContext sslContext = null;
    try{
        ClassLoader cl = this.getClass().getClassLoader();
        InputStream is = cl.getResourceAsStream("/cert/myApp.cert.pem");
        // You could get a resource as a stream instead.

        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate caCert = (X509Certificate)cf.generateCertificate(is);

        TrustManagerFactory tmf = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

        ks.load(null); // You don't need the KeyStore instance to come from a file.
        ks.setCertificateEntry("caCert", caCert);

        tmf.init(ks);

        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);
    } catch (Exception e){

    }
    return sslContext;
}
}

然后在我的路线建设者中:

@Component
public class SampleCamelRouter extends RouteBuilder {

@Autowired
SSLContext awsiotsslcontext;

@Override
public void configure() throws Exception {

    from("timer://foo?repeatCount=0&delay=5000&fixedRate=true&period=10s")
        .setBody(simple("TEST MESSAGE"))
        .to("mqtt:awsiot?host=ssl://{{aws.iot.host}}:8883&publishTopicName={{aws.iot.sub.topic}}&sslContext=#awsiotsslcontext").log("Sent :"+body().convertToString().toString());

    from("mqtt:awsiot?host=ssl://{{aws.iot.host}}:8883&subscribeTopicName={{aws.iot.sub.topic}}&sslContext=#awsiotsslcontext").log("Recieved : "+body().convertToString().toString());




}

}

收到以下错误:

  

java.lang.IllegalArgumentException:找不到合适的setter   for property:sslContext,因为没有setter方法   类型:java.lang.String或类型转换可能:没有类型转换器   可以从类型:java.lang.String转换为所需类型:   带有值#awsiotsslcontext

的javax.net.ssl.SSLContext

我认为这是一个简单的端点配置问题,但尝试了各种各样的事情似乎没有任何效果。拥有bean名称的#应该有驼峰在bean的注册表中查找但是在这里它将它识别为String?这里有解决方法吗?

1 个答案:

答案 0 :(得分:0)

这是驼峰路线配置的一个问题;当我在@Configuration下配置我的路由而不是按照文档的建议在@Component下配置时,它并没有用'#'来抱怨bean的定义。在URI中;我希望在Spring Boot Application中默认情况下bean会在路由之前加载:

    @Bean
    RouteBuilder awsIoTRoute() {

        return new RouteBuilder() {

            @Override
            public void configure() throws Exception {

                from("timer://foo?repeatCount=0&delay=5000&fixedRate=true&period=17s")
                    .setBody(simple("TEST MESSAGE"))
                    .to("mqtt:awsIoTPublisher?host=ssl://{{aws.iot.host}}:8883&publishTopicName={{aws.iot.pub.topic}}&clientId={{aws.iot.pub.clientId}}&sslContext=#sslContext")
                    .log("Sent :"+body().convertToString().toString());

                from("mqtt:awsIoTReciever?host=ssl://{{aws.iot.host}}:8883&subscribeTopicName={{aws.iot.sub.topic}}&clientId={{aws.iot.sub.clientId}}&sslContext=#sslContext").log("Recieved : "+body().convertToString());


            }
        };
    }