java camel exchange类_Exchange服务器之camel 基本概念
本文将带你了解Exchange服务器之camel 基本概念,希望本文对大家学Exchange有所帮助。
camel 对exchange的几种处理consuming方式:
1. bean
2. beanRef : beanRef("myTransformerBean"
, "myMethodName"
)
3.process:
.process(new
Processor() {
public
void process(Exchange exchange) {
Message in = exchange.getIn();
in.setBody(in.getBody(String
.class) + " World!"
);
}
})
4. transform :
.transform(body().append(" World!"))5.Templating (velocity , xquery):use InOut (request-reply) semantics:to process requests on the My.Queue queue on ActiveMQ with a template generated response,then sending responses back to the JMSReplyTo Destination you could use this: to("velocity:com/acme/MyResponse.vm")(1)velocity
Velocity
The velocity:
component allows you to process a message using an Apache Velocity
template. This can be ideal when using Templating
to generate responses for requests.
Maven users will need to add the following dependency to their pom.xml
for this component:
org.apache.camel
camel-velocity
x.x.x
URI format
velocity:templateName[?options]
Where templateName
is the classpath-local URI of the template to invoke; or the complete URL of the remote template (eg: file://folder/myfile.vm
).
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
Option
Default
Description
loaderCache
true
Velocity based file loader cache.
contentCache
New option in Camel 1.4: Cache for the resource content when it is loaded. By default, it's false
in Camel 1.x. By default, it's true
in Camel 2.x.
encoding
null
New option in Camel 1.6: Character encoding of the resource content.
propertiesFile
null
New option in Camel 2.1: The URI of the properties file which is used for VelocityEngine initialization.
Message Headers
The velocity component sets a couple headers on the message (you
can't set these yourself and from Camel 2.1 velocity component will not
set these headers which will cause some side effect on the dynamic
template support):
Header
Description
org.apache.camel.velocity.resource
Camel 1.x: The resource as an org.springframework.core.io.Resource
object.
org.apache.camel.velocity.resourceUri
Camel 1.x: The templateName
as a String
object.
CamelVelocityResource
Camel 2.0: The resource as an org.springframework.core.io.Resource
object.
CamelVelocityResourceUri
Camel 2.0: The templateName
as a String
object.
In Camel 1.4 headers set during the Velocity evaluation are returned
to the message and added as headers. Then its kinda possible to return
values from Velocity to the Message.
For example, to set the header value of fruit
in the Velocity template .tm
:
$in.setHeader('fruit', 'Apple')
The fruit
header is now accessible from the message.out.headers
.
Velocity Context
Camel will provide exchange information in the Velocity context (just a Map
). The Exchange
is transfered as:
key
value
exchange
The Exchange
itself.
headers
The headers of the In message.
camelContext
The Camel Context intance.
request
The In message.
in
The In message.
body
The In message body.
out
The Out message (only for InOut message exchange pattern).
response
The Out message (only for InOut message exchange pattern).
Hot reloading
The Velocity template resource is, by default, hot reloadable for both file and classpath resources (expanded jar). If you set contentCache=true
,
Camel will only load the resource once, and thus hot reloading is not
possible. This scenario can be used in production, when the resource
never changes.
Dynamic templates
Available as of Camel 2.1
Camel provides two headers by which you can define a different resource
location for a template or the template content itself. If any of these
headers is set then Camel uses this over the endpoint configured
resource. This allows you to provide a dynamic template at runtime.
Header
Type
Description
CamelVelocityResourceUri
String
Camel 2.1:
A URI for the template resource to use instead of the endpoint configured.
CamelVelocityTemplate
String
Camel 2.1:
The template to use instead of the endpoint configured.
Samples
For example you could use something like
from("activemq:My.Queue"). to("velocity:com/acme/MyResponse.vm");
To use a Velocity template to formulate a response to a message for InOut message exchanges (where there is a JMSReplyTo
header).
If you want to use InOnly and consume the message and send it to another destination, you could use the following route:
from("activemq:My.Queue"). to("velocity:com/acme/MyResponse.vm"). to("activemq:Another.Queue");
And to use the content cache, e.g. for use in production, where the .vm
template never changes:
from("activemq:My.Queue"). to("velocity:com/acme/MyResponse.vm?contentCache=true"). to("activemq:Another.Queue");
And a file based resource:
from("activemq:My.Queue"). to("velocity:file://myfolder/MyResponse.vm?contentCache=true"). to("activemq:Another.Queue");
In Camel 2.1
it's possible to specify what template the component should use dynamically via a header, so for example:
from("direct:in"). setHeader("CamelVelocityResourceUri").constant("path/to/my/template.vm"). to("velocity:dummy");
In Camel 2.1
it's possible to specify a template directly as a header the component should use dynamically via a header, so for example:
from("direct:in"). setHeader("CamelVelocityTemplate").constant("Hi this is a velocity template that can do templating ${body}"). to("velocity:dummy");
The Email Sample
In this sample we want to use Velocity templating for an order
confirmation email. The email template is laid out in Velocity as:
Dear ${headers.lastName}, ${headers.firstName}Thanks for the order of ${headers.item}.Regards Camel Riders Bookstore${body}
And the java code:
private Exchange createLetter() { Exchange exchange = context.getEndpoint("direct:a").createExchange(); Message msg = exchange.getIn(); msg.setHeader("firstName", "Claus"); msg.setHeader("lastName", "Ibsen"); msg.setHeader("item", "Camel in Action"); msg.setBody("PS: Next beer is on me, James");return exchange;}@Testpublic void testVelocityLetter() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedBodiesReceived("Dear Ibsen, Claus/n/nThanks for the order of Camel in Action./n/nRegards Camel Riders Bookstore/nPS: Next beer is on me, James"); template.send("direct:a", createLetter()); mock.assertIsSatisfied();}protected RouteBuilder createRouteBuilder() throws Exception {return new RouteBuilder() {public void configure() throws Exception { from("direct:a").to("velocity:org/apache/camel/component/velocity/letter.vm").to("mock:result"); } };}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标系统运维之Exchange频道!
java camel exchange类_Exchange服务器之camel 基本概念相关推荐
- java camel exchange类_让Camel处理各种URI类型
我想编写一个获取URI(可以是http,ftp,file,...)的Camel Route,然后获取数据并将其本地存储在文件中. 这个URI-String可以是,例如: "FTP://loc ...
- java设计一个bank类实现银行_用java定义一个类实现银行帐户的概念
满意答案 ZHIu6Az0v 推荐于 2017.11.26 采纳率:56% 等级:11 已帮助:8247人 /* * 2. 定义一个类实现银行帐户的概念,包括的变量有"帐号" ...
- java camel exchange类_camel
最近项目里会用到camel,近期会写一篇文章来总结camel中的一些常用的'知识点'. 总结一下. 1 from from("file://C:\\Users\\xxx\\documents ...
- 杨校老师课堂之Java项目部署到云端服务器之安装MySQL、Jdk、Tomcat
部署项目到公网上去思路剖析 · 公网服务器· 数据库· 下载.安装.配置.远程连接· 使用本地的Navicat去远程连接 操作数据库 · JDK 安装Tomcat的前提 · tomcat ·目录结构· ...
- JAVA基础––从类和对象开始
JAVA基础--从类和对象开始 一.概念 1.面向对象中类和对象的概念 对象:对象是具有状态和行为的实体,例如某个具体的学生,或者是王者荣耀里的英雄,可以是物理实体,也可以是逻辑实体. 类:类是一组具 ...
- camel发布多个服务
Apache Camel Endpoint同时发布多个服务 代码一: package com.camel.directcamel2;import java.io.InputStream;import ...
- java 连接OPC服务器之 utgard 连接 KepServer
java 连接OPC服务器之 utgard 连接 KepServer 我要做一个java开发的项目, 这个在网上很少案例, 大家基本都是做web开发的, 我其实之前也是.但是现在有这个需求, 就干了. ...
- java.exe闪退_tomcat7.exe闪退 解决点击startup.bat后闪退的情况 - 电脑常识 - 服务器之家...
tomcat7.exe闪退 解决点击startup.bat后闪退的情况 发布时间:2017-03-08 来源:服务器之家 最近开始学习jsp编程,当然在开始编程之前,我们要先把开发用的工具准备好,进行 ...
- 阿里云自动java和mysql数据库_阿里云服务器之基于Linux系统部署上线JavaWeb项目和连接MySQL数据库(从购买云服务器到发布JavaWeb项目全套详细流程)...
阿里云服务器之基于Linux系统部署上线JavaWeb项目和连接MySQL数据库(从购买云服务器到发布JavaWeb项目全套详细流程) (仅此纪念人生第一篇学习博客) 前阵子接了一个小小的JavaWe ...
最新文章
- Java遍历Map对象的四种方法
- java诡异的String.split()方法
- excel不能插入activex控件_办公小技巧:制作更炫酷的Excel下拉菜单
- java中使用openssl生成的rsa公私钥进行数据加解密_使用openssl生成RSA公钥和私钥对...
- 少年,看你异于常人,有空花2小时来参加有3000人的源码共读嘛~
- 计算机在学前教育和美术绘画中的应用,幼儿园美术教学活动中信息技术的应用...
- 程序员面试金典 - 面试题 16.11. 跳水板(数学)
- python爬app_Python爬虫抓取手机APP的传输数据
- [洛谷 P3992] [BJOI2017]开车
- 生物特征验证是什么意思
- Jquery实现滚动到底部加载更多(最原始)
- 数据库课程设计 ——酒店管理系统
- 二十款免费WiFi渗透工具
- 【数据结构】(森林)求以孩子兄弟表示法存储的森林叶子结点数
- 师范类大学计算机排名,2018中国师范类大学排行榜,北京师范大学第一
- 技术总监做技术经理的工作,技术经理做研发的工作是怎么回事?
- 支付宝、微信(pc+h5)支付
- 爱心跳动-HTML-抖音火的那个
- 爬取三千条数据需要多久_数字科学家赚多少钱?我爬取近 6 年三千份数据后发现了这些秘密...
- 由平庸到崛起:细数那些为三星打下半壁江山的经典“Galaxy”智能机型