[ 
https://issues.apache.org/jira/browse/CAMEL-9245?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14970655#comment-14970655
 ] 

Jaume Teixi commented on CAMEL-9245:
------------------------------------

Define two broker connections with PahoComponent bean id's in your Spring's 
Application Context
{code:xml}
        <bean id="paho" class="org.apache.camel.component.paho.PahoComponent">
                <property name="brokerUrl" value="tcp://broker.local:1883" />   
                        
                <property name="connectOptions">
                        <bean 
class="org.eclipse.paho.client.mqttv3.MqttConnectOptions">
                                <property name="userName" value="user@broker" />
                                <property name="password" value="password" />
                        </bean>
                </property>             
        </bean>
        
        <bean id="paho2" class="org.apache.camel.component.paho.PahoComponent">
                <property name="brokerUrl" value="tcp://broker2.local:1883" />  
                        
                <property name="connectOptions">
                        <bean 
class="org.eclipse.paho.client.mqttv3.MqttConnectOptions">
                                <property name="userName" value="user@broker2" 
/>
                                <property name="password" value="password2" />
                        </bean>
                </property>             
        </bean> 
{code}

Autowire the PahoComponent in your Java class. 
And implement two different methods for publishing to different topics for each 
one.

{code:java}
        @Autowired
        private ApplicationContext appContext;
        
        @Autowired
        private PahoComponent paho;
        
        @Autowired
        private PahoComponent paho2;

        public void publish() {
                try {
                        CamelContext camelContext = new 
SpringCamelContext(appContext);
                        camelContext.setAllowUseOriginalMessage(false);
                        camelContext.addComponent("paho", paho);
                        camelContext.start();
                        ProducerTemplate producer = 
camelContext.createProducerTemplate();
                        String payload = "just a message";
                        String clientId = "pub-paho-" + System.nanoTime();
                        
producer.sendBodyAndHeaders("paho:input/1?qos=1&clientId=" + clientId, 
payload.toString(), null);
                        camelContext.stop();
                } catch (Exception e) {
                        LOG.error("publish exception: {}", e.getMessage());
                }
        }
        
        public void publish2() {
                try {
                        CamelContext camelContext = new 
SpringCamelContext(appContext);
                        camelContext.setAllowUseOriginalMessage(false);
                        camelContext.addComponent("paho2", paho2);
                        camelContext.start();
                        ProducerTemplate producer = 
camelContext.createProducerTemplate();
                        String payload = "just a message 2";
                        String clientId = "pub-paho2-" + System.nanoTime();
                        
producer.sendBodyAndHeaders("paho2:input/2?qos=1&clientId=" + clientId, 
payload.toString(), null);
                        camelContext.stop();
                } catch (Exception e) {
                        LOG.error("publish2 exception: {}", e.getMessage());
                }               
        }
{code}

First method message: *just a message*
Is published +correctly+ into topic: *input/1*

Second method message: *just a message 2* 
Is published _wrongly_ into topic: */input/2*

> camel-paho - Endpoint should allow a flexible naming.
> -----------------------------------------------------
>
>                 Key: CAMEL-9245
>                 URL: https://issues.apache.org/jira/browse/CAMEL-9245
>             Project: Camel
>          Issue Type: Improvement
>          Components: camel-paho
>            Reporter: Jaume Teixi
>            Priority: Minor
>
> If endpoint doesn't match exactly paho: it fails to publish to the correct 
> topic.
> Once should be able in Spring to @Autowired two different PahoComponent 
> pointing to different Application Context defined PahoComponent bean id'S.
> Currently if bean id is not named exactly paho it fails to publish to the 
> correct topic and for example instead of input/1 publishes to t://input/1 
> when id is mypaho instead of paho



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to