camel配置(2)

作用简介:

作用我这里就不多做介绍了。

需求:

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);
	}


结语:这个配置相对来说简单很多,中间没有经过什么消息中间件之类的。比较直来直去,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用ConfigMap和Secrets配置Spring-Boot应用程序连接IBMMQ的步骤: 1.创建ConfigMap和Secrets对象,用于存储IBMMQ连接参数和身份验证信息。 2.在Spring-Boot应用程序的pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>x.x.x</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ibmmq-starter</artifactId> <version>x.x.x</version> </dependency> ``` 3.在application.yml文件中添加以下配置: ```yaml ibmmq: host: ${ibmmq.host} port: ${ibmmq.port} queue-manager: ${ibmmq.queue-manager} queue: ${ibmmq.queue} channel: ${ibmmq.channel} username: ${ibmmq.username} password: ${ibmmq.password} ccsid: ${ibmmq.ccsid} receive-timeout: ${ibmmq.receive-timeout} camel: url: ibmmq:queue:${ibmmq.queue}?useMessageIDAsCorrelationID=true mq: constant: queue:///TestQueue?targetClient=1 ``` 4.在Spring-Boot应用程序中创建一个Camel路由器,用于从IBMMQ队列中接收消息并将其发送到另一个队列。 ```java @Component public class MyRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { from("{{camel.url}}") .to("{{camel.mq.constant}}"); } } ``` 5.在Spring-Boot应用程序中创建一个CamelContext对象,并将Camel路由器添加到该对象中。 ```java @SpringBootApplication public class MyApp { @Autowired private MyRouteBuilder myRouteBuilder; public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @Bean public CamelContext camelContext() throws Exception { CamelContext camelContext = new DefaultCamelContext(); camelContext.addRoutes(myRouteBuilder); return camelContext; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值