Apache Camel - 从 jms 到 http
Apache Camel - from jms to http
我有一个使用 Apache Camel 的 spring-boot 项目。
我想从包含文件的 activemq 队列中读取消息并将其发送到 Web 服务器。
我正在尝试找到执行此操作的正确方法。
我相信我可以做出类似的东西:
from("activemq:queue").bean(MyBean.class, "process")
并手动构建一个 http 请求,但我不禁想到可能有更好的方法来做到这一点。喜欢:
from("activemq:queue").bean(MyBean.class, "process")
.setHeader(Exchange.HTTP_METHOD,constant("POST"))
.to("http://localhost:8080/test");
但我不知道如何操作 "exchange" 以获得有效的 http 消息。
MyBean 收到一个包含 JmsMessage 的 Exchange 对象。我看到还有一个 HTTPMessage,但我认为我不应该手动构建它。 (它需要 HTTPRequest 和 Response 对象,我不确定如何获取。)
有人能解释一下这个问题吗?
更新
我要去豆子解决方案。
from("activemq:queue").bean(MyBean.class, "sendMultipart");
public void sendMultipart(Exchange exchange) {
ByteArrayInputStream in = new ByteArrayInputStream((byte[]) exchange.getIn().getBody());
InputStreamBody contentBody = new InputStreamBody(in, ContentType.create("application/octet-stream"), "filename");
HttpEntity entity = MultipartEntityBuilder
.create()
.addPart("file", contentBody)
.build();
HttpPost httpPost = new HttpPost("http://localhost:8080/upload/");
httpPost.setEntity(entity);
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse);
} catch (IOException e) {
e.printStackTrace();
}
}
已更新post
我找到了这个 http://hilton.org.uk/blog/camel-multipart-form-data。它允许您利用 camel http 组件。
"jms:queue/SomeQ" ==> {
process(toMultipart)
setHeader(Exchange.CONTENT_TYPE, "multipart/form-data")
process((e: Exchange) => e.getIn.setHeader(Exchange.HTTP_URI,"http://localhost:8111/foo"))
to ("http:DUMMY")
}
def toMultipart(exchange: Exchange): Unit = {
val data = exchange.in[java.io.File]
val entity = MultipartEntityBuilder.create()
entity.addBinaryBody("file", data)
entity.addTextBody("name", "sample-data")
// Set multipart entity as the outgoing message’s body…
exchange.in = entity.build
}
旁注:这真的是一个很好的尝试反应流的用例。
原版post
我仍然无法理解您的实际问题。也许一些代码可能会有所帮助:
我现在假设您正在接收某种字符编码的字节,并希望将其发送到动态建立的 http 端点。
以下是您正在寻找的东西吗(代码在 camel 的 scala-dsl 中)
"jms:queue/SomeQ" ==> {
convertBodyTo(classOf[String],"UTF-32" )
process((e: Exchange) => e.in = e.in[String].toUpperCase + "!")
process((e: Exchange) => e.getIn.setHeader(Exchange.HTTP_URI,"http://localhost:8111/foo"))
to ("http:DUMMY")
}
它将作为 HTTP POST 发送,因为正文不为空。
我在为确保上面的代码正确而创建的另一个端点上收到了一切:
"jetty:http://localhost:8111/foo" ==> {
log("received on http 8111 endpoint ${body}")
}
我有一个使用 Apache Camel 的 spring-boot 项目。 我想从包含文件的 activemq 队列中读取消息并将其发送到 Web 服务器。
我正在尝试找到执行此操作的正确方法。
我相信我可以做出类似的东西:
from("activemq:queue").bean(MyBean.class, "process")
并手动构建一个 http 请求,但我不禁想到可能有更好的方法来做到这一点。喜欢:
from("activemq:queue").bean(MyBean.class, "process")
.setHeader(Exchange.HTTP_METHOD,constant("POST"))
.to("http://localhost:8080/test");
但我不知道如何操作 "exchange" 以获得有效的 http 消息。
MyBean 收到一个包含 JmsMessage 的 Exchange 对象。我看到还有一个 HTTPMessage,但我认为我不应该手动构建它。 (它需要 HTTPRequest 和 Response 对象,我不确定如何获取。)
有人能解释一下这个问题吗?
更新 我要去豆子解决方案。
from("activemq:queue").bean(MyBean.class, "sendMultipart");
public void sendMultipart(Exchange exchange) {
ByteArrayInputStream in = new ByteArrayInputStream((byte[]) exchange.getIn().getBody());
InputStreamBody contentBody = new InputStreamBody(in, ContentType.create("application/octet-stream"), "filename");
HttpEntity entity = MultipartEntityBuilder
.create()
.addPart("file", contentBody)
.build();
HttpPost httpPost = new HttpPost("http://localhost:8080/upload/");
httpPost.setEntity(entity);
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse);
} catch (IOException e) {
e.printStackTrace();
}
}
已更新post
我找到了这个 http://hilton.org.uk/blog/camel-multipart-form-data。它允许您利用 camel http 组件。
"jms:queue/SomeQ" ==> {
process(toMultipart)
setHeader(Exchange.CONTENT_TYPE, "multipart/form-data")
process((e: Exchange) => e.getIn.setHeader(Exchange.HTTP_URI,"http://localhost:8111/foo"))
to ("http:DUMMY")
}
def toMultipart(exchange: Exchange): Unit = {
val data = exchange.in[java.io.File]
val entity = MultipartEntityBuilder.create()
entity.addBinaryBody("file", data)
entity.addTextBody("name", "sample-data")
// Set multipart entity as the outgoing message’s body…
exchange.in = entity.build
}
旁注:这真的是一个很好的尝试反应流的用例。
原版post
我仍然无法理解您的实际问题。也许一些代码可能会有所帮助:
我现在假设您正在接收某种字符编码的字节,并希望将其发送到动态建立的 http 端点。
以下是您正在寻找的东西吗(代码在 camel 的 scala-dsl 中)
"jms:queue/SomeQ" ==> {
convertBodyTo(classOf[String],"UTF-32" )
process((e: Exchange) => e.in = e.in[String].toUpperCase + "!")
process((e: Exchange) => e.getIn.setHeader(Exchange.HTTP_URI,"http://localhost:8111/foo"))
to ("http:DUMMY")
}
它将作为 HTTP POST 发送,因为正文不为空。
我在为确保上面的代码正确而创建的另一个端点上收到了一切:
"jetty:http://localhost:8111/foo" ==> {
log("received on http 8111 endpoint ${body}")
}