在本文中,我们将学习如何使用多个 Camel JMSComponent 对象来桥接两个代理之间的消息。
为了用 Camel 实现两个不同的 broker 之间的集成,需要定义多个 JMSComponent 对象:只需添加 2 个方法并给每个方法一个不同的名字,方法的名字在 spring 的时候默认是 bean id 使用@Bean。 例如,在这个 @Configuration 类中,我们定义了一个 JMSComponent(名为“activemq”)以与 Artemis MQ 集成,另一个名为“wmq”以与 IBM MQ 集成:
import com.ibm.mq.jms.*;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.camel.component.jms.JmsComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;
@Configuration public class AppConfig {
@Bean public JmsComponent activemq() throws Exception {
// Create the connectionfactory which will be used to connect to Artemis
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
cf.setBrokerURL("tcp://localhost:61616");
cf.setUser("admin");
cf.setPassword("admin");
// Create the Camel JMS component and wire it to our Artemis connectionfactory
JmsComponent jms = new JmsComponent();
jms.setConnectionFactory(cf);
return jms;
}
@Bean public JmsComponent wmq() {
JmsComponent jmsComponent = new JmsComponent();
jmsComponent.setConnectionFactory(mqQueueConnectionFactory());
return jmsComponent;
}
@Bean public MQQueueConnectionFactory mqQueueConnectionFactory() {
MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
mqQueueConnectionFactory.setHostName("localhost");
try {
mqQueueConnectionFactory.setTransportType(1);
mqQueueConnectionFactory.setChannel("EXTAPP.SRVCONN");
mqQueueConnectionFactory.setPort(1414);
mqQueueConnectionFactory.setQueueManager("QMGRSCORE");
} catch (Exception e) {
e.printStackTrace();
}
return mqQueueConnectionFactory;
}
@Bean public UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(MQQueueConnectionFactory mqQueueConnectionFactory) {
UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
userCredentialsConnectionFactoryAdapter.setUsername("username");
userCredentialsConnectionFactoryAdapter.setPassword("password");
userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
return userCredentialsConnectionFactoryAdapter;
}
}
另外,请注意,@Bean 也有一个 id/name 属性,您也可以设置而不是使用方法名称。 使用上面的 JMSComponent 对象,您现在可以在 Routes 中引用它们,如下例所示:
package com.sample;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class CamelArtemisRouteBuilder extends RouteBuilder {
public void configure() throws Exception {
from("timer:mytimer?period=5000")
.routeId("generate-route")
.transform(constant("HELLO from Camel!"))
.to("activemq:queue:QueueIN");
from("activemq:queue:QueueIN")
.routeId("receive-route")
.log("Received a message - ${body} - sending to outbound queue")
.to("wmq:queue:QueueOUT?exchangePattern=InOnly");
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
内容来源于网络,如有侵权,请联系作者删除!