【问题标题】:Camel MQTT Configuration options DSL route URL parameter with bean value not working带有 bean 值的 Camel MQTT 配置选项 DSL 路由 URL 参数不起作用
【发布时间】:2017-08-08 23:33:40
【问题描述】:

我正在尝试从外部代理 (AWs IoT) 测试发布/订阅;从camel-example-spring-boot 示例项目开始,并添加了camel-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:找不到合适的设置器 for property: sslContext 因为没有相同的 setter 方法 类型:java.lang.String 也不能进行类型转换:没有类型转换器 可从 type: java.lang.String 转换为所需的类型: javax.net.ssl.SSLContext,值为 #awsiotsslcontext

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

【问题讨论】:

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


    【解决方案1】:

    这是骆驼路线配置的问题;当我在 @Configuration 下配置我的路由而不是按照文档的建议在 @Component 下配置时,它不会抱怨 URI 中带有“#”的 bean 定义;我希望在 Spring Boot 应用程序中,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());
    
    
                }
            };
        }
    

    【讨论】:

      猜你喜欢
      • 2013-04-14
      • 2016-10-06
      • 2017-05-08
      • 2015-08-13
      • 2020-07-15
      • 1970-01-01
      • 2019-03-21
      • 2012-12-19
      • 1970-01-01
      相关资源
      最近更新 更多