`

利用Camel调用远程对象

阅读更多
利用Camel调用远程对象,很简答也很方便,只要定义好服务器和客户端即可。下面的例子可以直接运行,不过需要导入Camel和ActiveMQ的Jar包, 所有的Jar包都能在camel.apache.org下载。
代码如下:
1.先定义接口:
import java.util.Date;

public interface StudentInterface {
	
	public String getName();
	
	public int getAge();
	
	public Date getDate();
}

2.定义实现类:
public class StudentImpl implements StudentInterface {

	@Override
	public String getName() {
		return "Tom";
	}

	@Override
	public int getAge() {
		return 11;
	}

	@Override
	public Date getDate() {
		return new Date();
	}

}

3.定义服务器端配置文件:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:camel="http://camel.apache.org/schema/spring" xmlns:broker="http://activemq.apache.org/schema/core"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
        http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd">

	<camel:camelContext id="camel-server">
		<camel:route>
			<camel:from uri="jms:queue:getStudent" />
			<camel:to uri="studentExport" />
		</camel:route>
	</camel:camelContext>

	<broker:broker useJmx="false" persistent="false"
		brokerName="localhost">
		<broker:transportConnectors>
			<broker:transportConnector name="tcp"
				uri="tcp://localhost:61610" />
		</broker:transportConnectors>
	</broker:broker>

	<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
		<property name="brokerURL" value="tcp://localhost:61610" />
	</bean>

	<bean id="studentExport" class="org.apache.camel.spring.remoting.CamelServiceExporter">
		<property name="uri" value="jms:queue:getStudent" />
		<property name="service" ref="student" />
		<property name="serviceInterface" value="com.zakisoft.camel.demo01.service.inft.StudentInterface" />
	</bean>

	<bean id="student" class="com.zakisoft.camel.demo01.service.impl.StudentImpl" />

</beans>

4:定义客户端配置文件
<?xml version="1.0" encoding="UTF-8"?>

<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.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <camel:camelContext id="camel-client">
    	<camel:template id="camelTemplate"/>
          <camel:proxy
            id="studentProxy"
            serviceInterface="com.zakisoft.camel.demo01.service.inft.StudentInterface"
            serviceUrl="jms:queue:getStudent"/>
    </camel:camelContext>
    
    <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="brokerURL" value="tcp://localhost:61610"/>
    </bean>
    
</beans>

5.定义客户端和测试代码:
package com.zakisoft.camel.demo01.service;

import static org.junit.Assert.assertEquals;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zakisoft.camel.demo01.service.inft.StudentInterface;

public class CamelRemote4Test {

	protected static ConfigurableApplicationContext serverContext;

	@BeforeClass
	public static void setUpServer() {
		serverContext = new ClassPathXmlApplicationContext("config01/camel-server.xml");
	}

	@AfterClass
	public static void tearDownServer() {
		if (serverContext != null) {
			serverContext.stop();
		}
	}

	@Test
	public void testCamelRemotingInvocation() {
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
				"config01/camel-client-remoting.xml");
		
		StudentInterface student = (StudentInterface) context.getBean("studentProxy");

		int age = student.getAge();

		assertEquals("Get a wrong response", 11, age);

		context.stop();
	}
}


6. 所有源文件都在附件中。
分享到:
评论

相关推荐

    camel-cxf调用和发布webservice例子

    用camel-cxf调用webservice和发布一个webservice接口例子,首先启动QueryServiceMain主函数启动webservice接口,然后启动测试类TestWebservice。例子主要是实现java代码实现camel调用webservice接口

    使用CXF和camel-cxf调用webservice

    NULL 博文链接:https://liugang594.iteye.com/blog/2022626

    Camel in action(camel实战)

    Apache Camel is a Java framework that lets you implement the standard enterprise integration patterns in a few lines of code. With a concise but sophisticated DSL you snap integration logic into your ...

    Camel_Camel3Camel6函数_

    Camel3 Camel6函数等matlab源代码

    [Camel实战].(Camel.in.Action).Claus.Ibsen&Jonathan;.Anstey.文字版

    中文名: Camel 实战 原名: Camel in Action 作者: Claus Ibsen Jonathan Anstey 资源格式: PDF 版本: 英文文字版/更新源代码 出版社: Manning书号: 9781935182368发行时间: 2010年12月 地区: 美国 语言: 英文 简介: ...

    ApacheCamel-JDBC

    ApacheCamel-JDBC Apache Camel JDBC组件 代码样例Demo

    apache-camel-demo

    Apache Camel是一个基于规则路由和中介引擎,提供企业集成模式的Java对象(POJO)的实现,通过应用程序接口(或称为陈述式的Java领域特定语言(DSL))来配置路由和中介的规则。领域特定语言意味着Apache Camel支持你...

    Apache Camel中文开发使用指南.zip

    Apache Camel 开发使用指南中文版

    Camel服务集成,服务编排操作文档

    Camel服务集成,服务编排操作文档

    ApacheCamel-FTP

    ApacheCamel-FTP ApacheCamel-FTP Apache Camel FTP组件 Demo 样例

    camel, Apache camel 镜像.zip

    camel, Apache camel 镜像 Apache camel 是基于已知企业集成模式的强大开放源代码集成框架,它具有强大的Bean集成。简介flex允许你创建企业集成模式,以基于基于Java的域特定语言( 或者 Fluent API ) 或者基于 Sc

    ApacheCamel-Timer

    09-ApacheCamel-Timer Apache Camel Timer组件 定时器 代码Demo

    camel-manual-2.0

    camel-manual-2.0 camel ESB EIP EAI

    Camel in Action ch1

    1: Meet Camel - FREE 2: Routing with Camel - AVAILABLE Part 2 Core Camel 3: Transforming Data with Camel - AVAILABLE 4: Using Beans with Camel - AVAILABLE 5: Error Handling - AVAILABLE 6:...

    camel文档

    camel 文档

    Mastering.Apache.Camel

    An advanced guide to Enterprise Integration using Apache Camel About This Book Integrate your applications with Apache Camel and enhance efficiency and scalability Master all the EIPs supported by ...

    Apache Camel 源码分析.rar

    camel direct http jdbc mybatis 等等组件 骆驼 camel部分源码分析

    Camel实战中文版第四章.pdf

    Camel In Action一书第四章的中文版。

    Camel in Action.zip

    Apache Camel 作为集成项目的利器,针对应用集成场景的抽象出了一套消息交互模型,通过组件的方式进行第三方系统的接入,目前Apache Camel已经提供了300多种组件能够接入HTTP,JMS,TCP,WS-*,WebSocket 等多种传输...

Global site tag (gtag.js) - Google Analytics