简体   繁体   English

使用骆驼通过HTTP进行文件传输

[英]File transfer over HTTP using camel

I'm trying to set up a Camel route for transferring files over HTTP. 我正在尝试建立一个通过HTTP传输文件的骆驼路由。 I'm also trying to understand the concept as I'm new to this. 我还试图理解这个概念,因为我对此并不陌生。

When I code something like below, does that mean I'm routing a simple message over HTTP? 当我编写如下代码时,是否表示我正在通过HTTP路由一条简单消息? Could I call Jetty the consumer in this case? 在这种情况下,我可以叫Jetty为消费者吗? I'm able to run the below code and call the browser and see the message successfully. 我可以运行以下代码并调用浏览器,然后成功看到该消息。

    from("jetty://http://localhost:32112/greeting")
    .setBody(simple("Hello, world!"));

However, I want to send a simple message(eventually an XML) over HTTP following which I would want to save it on disk and analyse it further. 但是,我想通过HTTP发送一条简单的消息(最终是XML),然后将其保存在磁盘上并进行进一步分析。 Should the code like below work? 像下面的代码应该工作吗?

CamelContext context = new DefaultCamelContext();
ProducerTemplate template = context.createProducerTemplate();
template.sendBody("direct:start", "This is a test message");
from("direct:start")
.to("jetty://localhost:32112/greeting");

from("jetty://http://localhost:32112/greeting")
.to("direct:end");

Should I be not using direct:start here for parsing XMLs? 我是否应该不使用direct:start从此处解析XML?

Thanks a lot for the help. 非常感谢您的帮助。

first you have to create your routes and start your context. 首先,您必须创建路线并开始上下文。 Then you can send messages via your template. 然后,您可以通过模板发送消息。

The route could look like this from("jetty:http://0.0.0.0:32112/greeting") .routeId("xml-converter-route").autoStartup(false) .bean(xmlConverterBean, "convertXmlMethodToBeCalledInBean()") ; from("jetty:http://0.0.0.0:32112/greeting") .routeId("xml-converter-route").autoStartup(false) .bean(xmlConverterBean, "convertXmlMethodToBeCalledInBean()") ;

If you just want to transfer data and nothing else use restlet or netty-http4. 如果您只想传输数据,而没有其他用途,请使用restlet或netty-http4。 More lightweight than jetty. 比码头更轻巧。

from("restlet:/http://localhost:32112/greeting").convertBodyTo(String.class).log(LoggingLevel.INFO, "filetransfer", "log of body: ${body} and headers ${headers}").to("file://C:/test?fileName=body.txt");

Here's a camel test which may help you understand how these components work. 这是一个骆驼测试,可以帮助您了解这些组件的工作方式。

public class CamelRESTExampleTest extends CamelTestSupport {
    Logger LOG = LoggerFactory.getLogger(CamelRESTExampleTest.class);

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {

                // Create a service listening on port 8080
                from("restlet:http://localhost:8080/xmlFileService?restletMethod=post")
                        .process(new Processor() {
                            public void process(Exchange exchange) throws Exception {
                                String rawXML = exchange.getIn().getBody(String.class);
                                LOG.info("rawXML=" + rawXML);
                            }
                        });

                // Read files from the local directory and send to the service.
                // Create a test.xml file in this directory and it will be read in
                from("file:src/test/resources/data?noop=true")
                        .to("restlet:http://localhost:8080/xmlFileService?restletMethod=post");
            }
        };
    }

    @Test
    public void test() throws InterruptedException {
        // Give the route time to complete
        TimeUnit.SECONDS.sleep(5);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM