将文件路由到队列Apache Camel

时间:2017-01-06 11:46:59

标签: java routing apache-camel

我需要从文件夹中读取文件并将其放在队列中。

这是我的代码:

import java.io.File;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class FileMove{
    public static void main(final String[] arguments) {
        final CamelContext camelContext = new DefaultCamelContext();
        try {
            camelContext.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {

                    from("file://E:/TestingWatch1/input?preMove=staging&move=.completed&scheduler=quartz2&scheduler.cron=00+*/1+*+1/1+*+?+*")
                            .routeId("testRoute").process(new Processor() {
                                @Override
                                public void process(Exchange msg) throws Exception {
                                    File file = msg.getIn().getBody(File.class);
                                    String file1 = msg.getIn().getBody(String.class);
                                    System.out.println("" + file + file1);

                                }

                            }).
                            to("file://E:/TestingWatch1/output");
                }
            });
            camelContext.start();
            // Thread.sleep(10*60*1000);
            // camelContext.stop();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

我能够读取文件和文件内容,但我需要将它放在队列中。

如何使用Java DSL将文件或文件内容移动到队列?

1 个答案:

答案 0 :(得分:1)

谢谢,我找到了解决方案:这是代码

import java.io.File;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

public class CopyTest {
    public static void main(final String[] arguments) {
        final CamelContext camelContext = new DefaultCamelContext();
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin",
                ActiveMQConnection.DEFAULT_BROKER_URL);
        camelContext.addComponent("test-jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
        try {
            camelContext.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {

                    from("file://E:/TestingWatch1/input?preMove=staging&move=.completed&scheduler=quartz2&scheduler.cron=00+*/1+*+1/1+*+?+*")
                            .routeId("testRoute").process(new Processor() {
                                @Override
                                public void process(Exchange msg) throws Exception {
                                    File file = msg.getIn().getBody(File.class);
                                    String file1 = msg.getIn().getBody(String.class);
                                    System.out.println("" + file + file1);

                                }

                            }).to("test-jms:queue:testMQDestination").
                            to("file://E:/TestingWatch1/output");
                }
            });
            camelContext.start();
            // Thread.sleep(10*60*1000);
            // camelContext.stop();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}