Apache Camel框架之JMS路由

17 篇文章 0 订阅
11 篇文章 2 订阅

继上次Camel如何在做项目集成类型的项目中用于从FTP取文件和传文件之后,我们在系统集成中经常遇到的另一个应用就是将数据通过JMS传到消息中间件的queue里,或者从消息中间件的queue里取消息.

本文简单的介绍和示例一个用Camel实现这样的需求:监听某一个文件夹是否有文件,取到文件后发送到另外一个系统监听的queue.(图片来源于Camel in Action)

1,因为要用JMS,这里介绍一个open source的activeMQ,可以从http://activemq.apache.org/download.html 下载,下载后解压,bin目录有一个activemq.bat文件,在命令行里运行activemq 启动activeMQ,如果能从从浏览器里访问 http://localhost:8161/admin/则activeMQ成功启动了.

2,在Camel里实现上图所示的路由:JAVA项目里需要将activeMQ的jar包配置到classpath下,Java代码如下:

  1. private static String user = ActiveMQConnection.DEFAULT_USER;
  2. private static String password = ActiveMQConnection.DEFAULT_PASSWORD;
  3. private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
  4. public static void main(String args[]) throws Exception {
  5. CamelContext context = new DefaultCamelContext();
  6. ConnectionFactory connectionFactory =
  7. new ActiveMQConnectionFactory(user, password, url);
  8. context.addComponent("jms",
  9. JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
  10. System.out.println(url + " " + user + password);
  11. context.addRoutes(new RouteBuilder() {
  12. public void configure() {
  13. from("file:d:/temp/inbox").to(
  14. "jms:queue:TOOL.DEFAULT");
  15. }
  16. });
  17. context.start();
  18. boolean loop = true;
  19. while (loop) {
  20. Thread.sleep(25000);
  21. }
  22. context.stop();
  23. }
    private static String user = ActiveMQConnection.DEFAULT_USER;
    private static String password = ActiveMQConnection.DEFAULT_PASSWORD;
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    
    public static void main(String args[]) throws Exception {        
        CamelContext context = new DefaultCamelContext();        
        ConnectionFactory connectionFactory = 
            new ActiveMQConnectionFactory(user, password, url);
        context.addComponent("jms",
            JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
        System.out.println(url + " " + user + password);        
        context.addRoutes(new RouteBuilder() {
            public void configure() {                
                from("file:d:/temp/inbox").to(
                "jms:queue:TOOL.DEFAULT");
            }
        });
        context.start();
        boolean loop = true;
        while (loop) {
            Thread.sleep(25000);
        }

        context.stop();
    }

Camel会在路由的时候将文件的内容以binary message发到activeMQ的名为'TOOL.DEFAULT'的queue .

当然也可以用如下的方式发送textmessage:from("file:d:/temp/inbox").convertBodyTo(String.class).to("jms:queue:TOOL.DEFAULT");

用下面的代码可以从Camel发送的queue里取到消息.

  1. private static String user = ActiveMQConnection.DEFAULT_USER;
  2. private static String password = ActiveMQConnection.DEFAULT_PASSWORD;
  3. private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
  4. private static boolean transacted;
  5. private static int ackMode = Session.AUTO_ACKNOWLEDGE;
  6. public static void main(String[] args) throws Exception {
  7. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
  8. Connection connection = connectionFactory.createConnection();
  9. connection.start();
  10. Session session = connection.createSession(transacted, ackMode);
  11. Destination destination = session.createQueue("TOOL.DEFAULT");
  12. MessageConsumer consumer = session.createConsumer(destination);
  13. Message message = consumer.receive(1000);
  14. if (message instanceof TextMessage) {
  15. TextMessage txtMsg = (TextMessage) message;
  16. System.out.println("Received Text message : " + txtMsg.getText());
  17. } else if(message != null){
  18. BytesMessage bytesMsg = (BytesMessage) message;
  19. byte[] bytes = new byte[(int) bytesMsg.getBodyLength()];
  20. bytesMsg.readBytes(bytes);
  21. System.out.println("Received byte message: " + new String(bytes));
  22. }
  23. consumer.close();
  24. session.close();
  25. connection.close();
  26. }
    private static String user = ActiveMQConnection.DEFAULT_USER;
    private static String password = ActiveMQConnection.DEFAULT_PASSWORD;
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    private static boolean transacted;
    private static int ackMode = Session.AUTO_ACKNOWLEDGE;

    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(transacted, ackMode);
        Destination destination = session.createQueue("TOOL.DEFAULT");
        MessageConsumer consumer = session.createConsumer(destination);
        Message message = consumer.receive(1000);
        if (message instanceof TextMessage) {
            TextMessage txtMsg = (TextMessage) message;
            System.out.println("Received Text message : " + txtMsg.getText());
        } else if(message != null){
            BytesMessage bytesMsg = (BytesMessage) message;
            byte[] bytes = new byte[(int) bytesMsg.getBodyLength()];
            bytesMsg.readBytes(bytes);
            System.out.println("Received byte message: " + new String(bytes));
        }
        consumer.close();
        session.close();
        connection.close();
    }

同样,上面的路由也可以通过Spring配置实现:

  1. <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
  2. <property name="connectionFactory">
  3. <bean class="org.apache.activemq.ActiveMQConnectionFactory">
  4. <property name="brokerURL" value="failover://tcp://localhost:61616"/>
  5. </bean>
  6. </property>
  7. </bean>
  8. <camelContext xmlns="http://camel.apache.org/schema/spring">
  9. <route>
  10. <from uri="file:d:/temp/inbox"/>
  11. <to uri="jms:queue:TOOL.DEFAULT"/>
  12. </route>
  13. </camelContext>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值