标签:
作用简介:
作用我这里就不多做介绍了。
需求:
2个实例需求:
(1)来源tcp,xml格式数据,经过转换为json数据,发送到指定的http地址中。
(2)来源tcp,xml格式数据,经过转换为json数据,发送到指定的http地址中,等待http给出响应json数据,将响应的json数据转换为xml格式字符串响应到来源tcp
1.xml配置文件,这个配置很简单。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <bean id="fromBosTransform" class="com.lakala.loan.exchange.listener.transform.FromBosTransform"/> <bean id="fromNetTransform" class="com.lakala.loan.exchange.listener.transform.FromNetTransform"/> <camel:camelContext id="camel" trace="false" xmlns="http://camel.apache.org/schema/spring"> <!-- 该实例的需求:监听该端口号是否有消息。tcp传输xml字符串,转换为json字符串发送到对应的http中 --> <route id="toloanbus"> <from uri="mina2:tcp://127.0.0.1:11113?textline=true&sync=false&timeout=200000&allowDefaultCodec=false"/> <!-- 在 fromBosTransform类中bosToNet方法中,将接收到的数据转换为json数据 --> <bean ref="fromBosTransform" method="bosToNet"/> <!-- http接口格式要求为json数据 --> <setHeader headerName="Content-Type"> <constant>application/json</constant> </setHeader> <!-- 将数据发送到知道的http地址中 --> <to uri="http://127.0.0.1:8080/usermanag/hello.do?httpClient.soTimeout=5000&httpClient.connectionManagerTimeout=500000"/> <to uri="mock:results"/> </route> <!-- 该实例的需求:监听该端口号,tcp传输xml字符串,转换为json字符串发送到对应http请求中,然后处理完成后返回json字符串 这边转换为xml格式,响应回来源tcp地址中 --> <route id="toloanbus1"> <from uri="mina2:tcp://127.0.0.1:11114?textline=true&sync=true&timeout=200000"/> <!-- 将接收到的数据转换为json数据 --> <bean ref="fromBosTransform" method="bosToNet"/> <!-- http接口格式要求为json数据 --> <setHeader headerName="Content-Type"> <constant>application/json</constant> </setHeader> <to ref="http://127.0.0.1:8080/usermanag/hello.do?httpClient.soTimeout=5000&httpClient.connectionManagerTimeout=500000"/> <to uri="mock:results"/> <!-- http响应回来的json数据 --> <setHeader headerName="Content-Type"> <!-- 响应tcp格式是xml,如果不是则不进行返回 --> <constant>application/xml;charset=utf-8</constant> </setHeader> <transform> <!-- 在fromBosTransform类中getTradeResult方法将json数据转换为xml格式字符串 --> <method ref="fromBosTransform" method="getTradeResult"/> </transform> </route> </camel:camelContext> </beans>2.贴一个<bean ref="fromBosTransform" method="bosToNet" />实例,给新接触camel的一个接收数据转换的实例
public void bosToNet(Exchange exchange, @Body String body, @Header(value = "Content-Type") String contentType) throws Exception { //body是接收节点传输的数据 //将xml数据转换为map,详细代码不进行贴出了。百度一搜好多 Map map=XmlToMap.xml2Map(body.substring(6)); //private ObjectMapper mapper; //mapper.writeValueAsString(map)将map转换为json数据,camel自带的数据转换 exchange.getOut().setBody(mapper.writeValueAsString(map),String.class); }
标签:
原文地址:http://blog.csdn.net/ncq_1993/article/details/51363569