`
Everyday都不同
  • 浏览: 721239 次
  • 性别: Icon_minigender_1
  • 来自: 宇宙
社区版块
存档分类
最新评论

Camel流程扩展探索——多个流程是否可行?

阅读更多

camel框架是一个成熟的流程框架,一般而言我们只是把它应用到一个完整的流程中,而一些逻辑的分支也是在“一个”流程中去控制的。现在如果在流程的源头就需要分支,即拿到源数据,

但是我们需要走不同的流程。(这里不再局限在“一个”流程了!),是否可行呢?下面来探讨。

 

首先,建立流程的配置:

context-route.xml配置文件

<?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:drools="http://drools.org/schema/drools-spring"
	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.0.xsd
http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
	
	<bean id="fileConverter" class="com.kayak.itmon.web.demo.camel.FileConvertProcessor" />
	<bean id="fileConverter2" class="com.kayak.itmon.web.demo.camel.FileConvertProcessor2" />
	<bean id="fileConverter3" class="com.kayak.itmon.web.demo.camel.FileConvertProcessor3" />
	
	<camelContext id="camel" autoStartup="true" xmlns="http://camel.apache.org/schema/spring">
		 <template id="producerTemplate" />
		<threadPool id="pool" threadName="Thread-dataformat"
			poolSize="50" maxPoolSize="200" maxQueueSize="250" rejectedPolicy="CallerRuns" />
		
		 
		 <route>  
            <from uri="direct://AstartData"/>  
            <process ref="A1"/>  
            <to uri="direct://startA" />
            
        </route> 
        
         <route> 
	        <from uri="direct://startA"/>  
         	<threads executorServiceRef="pool">
         		<process ref="A2" />
         	</threads> 
        </route>
        
        <!-- B流程 -->
         <route>  
            <from uri="direct://BstartData"/>  
            <process ref="B1"/>  
            <to uri="direct://startB" />
        </route> 
        
         <route> 
	        <from uri="direct://startB"/>  
         	<threads executorServiceRef="pool">
         		<process ref="B2" />
         	</threads> 
        </route>
	</camelContext>
	
	<!-- camel的一些bean -->
	<bean id="A1" class="com.kayak.itmon.web.demo.camel.ACamel1"></bean>
	<bean id="A2" class="com.kayak.itmon.web.demo.camel.ACamel2"></bean>
	<bean id="B1" class="com.kayak.itmon.web.demo.camel.BCamel1"></bean>
	<bean id="B2" class="com.kayak.itmon.web.demo.camel.BCamel2"></bean>
</beans>

 

其中,流程类如下

public class ACamel1 implements Processor {

	@Override
	public void process(Exchange exchange) throws Exception {
		String ret = (String) exchange.getIn().getBody();
		System.out.println("进入到A流程的第一步。。 " + ret);
		ret = ret.toUpperCase();
		exchange.getIn().setBody(ret);
	}

}

public class ACamel2 implements Processor {

	@Override
	public void process(Exchange exchange) throws Exception {
		String ret = (String) exchange.getIn().getBody();
		System.out.println("进入到A流程的第二步。。" + ret);
	}

}

public class BCamel1 implements Processor {

	@Override
	public void process(Exchange exchange) throws Exception {
		String ret = (String) exchange.getIn().getBody();
		System.out.println("进入到B流程的第一步。。  " + ret);
		ret = ret + "hahha";
		exchange.getIn().setBody(ret);
	}

}

public class BCamel2 implements Processor {

	@Override
	public void process(Exchange exchange) throws Exception {
		String ret = (String) exchange.getIn().getBody();
		System.out.println("进入到B流程的第二步。。 " + ret);
	}

}

 

在测试类里发起流程:

public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/conf/xxxxx/context-route.xml");
		System.out.println(ctx);
		
		ProducerTemplate template = ctx.getBean("producerTemplate", ProducerTemplate.class);
		//template.sendBody("direct://AstartData");
		template.sendBody("direct://AstartData", "okoko");
        template.sendBody("direct://BstartData", "okoko");
		try {
			template.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
}

 

会发现,打印结果如下:

进入到A流程的第一步。。 okoko

进入到A流程的第二步。。OKOKO

进入到B流程的第一步。。  okoko

进入到B流程的第二步。。 okokohahha

 

说明,同一个数据源(这个demo用的是okoko模拟),可以“分发”给若干个(这里是用2个测试)流程来处理,且流程与流程之间拿到的同一数据源,

但他们的处理互不干涉。只要在ProducerTemplate.sendBody("direct://xxx", data); send多个即可

(注:不要嵌套流程,否则容易出错。即在本例中,

<route>  
            <from uri="direct://AstartData"/>  
            <process ref="A1"/>  
            <to uri="direct://startA" />
            <to uri="direct://BstartData" />
</route>

 

构成了嵌套,这是不合理的!direct://AstartData和direct://BstartData(这两个都是流程的源头uri)都应该在java类中启动!)

 

btw, 说下在测试的过程中,出现的一些异常及解决办法吧,记录一下。

 

1)xml里from uri="xxx"里面要加direct://前缀  形如from uri="direct://xxx", 否则会出现异常:

Exception in thread "main" org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToStartRouteException: Failed to start route route4 because of Multiple consumers for the same endpoint is not allowed: Endpoint[direct://start]

at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1316)

at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:120)

at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:280)

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:773)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)

at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)

at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)

at com.kayak.itmon.web.demo.camel.CamelStart.main(CamelStart.java:9)

Caused by: org.apache.camel.FailedToStartRouteException: Failed to start route route4 because of Multiple consumers for the same endpoint is not allowed: Endpoint[direct://start]

at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRouteConsumers(DefaultCamelContext.java:2019)

at org.apache.camel.impl.DefaultCamelContext.doStartRouteConsumers(DefaultCamelContext.java:1995)

at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:1923)

at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:1702)

at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1583)

at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1444)

at org.apache.camel.spring.SpringCamelContext.doStart(SpringCamelContext.java:179)

at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)

at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1412)

at org.apache.camel.spring.SpringCamelContext.maybeStart(SpringCamelContext.java:228)

at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:118)

... 9 more

 

2)不要有相同的uri="direct://xxx"  否则会出现异常:

Exception in thread "main" org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route1: Route[[From[AstartData]] -> [process[ref:A1], To[direct://st... because of No endpoint could be found for: AstartData, please check your classpath contains the needed Camel component jar.

at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1316)

at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:120)

at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:280)

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:773)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)

at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)

at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)

at com.kayak.itmon.web.demo.camel.CamelStart.main(CamelStart.java:9)

Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route route1: Route[[From[AstartData]] -> [process[ref:A1], To[direct://st... because of No endpoint could be found for: AstartData, please check your classpath contains the needed Camel component jar.

at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:177)

at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:722)

at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:1789)

at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1575)

at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1444)

at org.apache.camel.spring.SpringCamelContext.doStart(SpringCamelContext.java:179)

at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)

at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1412)

at org.apache.camel.spring.SpringCamelContext.maybeStart(SpringCamelContext.java:228)

at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:118)

... 9 more

Caused by: org.apache.camel.NoSuchEndpointException: No endpoint could be found for: AstartData, please check your classpath contains the needed Camel component jar.

at org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:52)

at org.apache.camel.model.RouteDefinition.resolveEndpoint(RouteDefinition.java:187)

at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:108)

at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:114)

at org.apache.camel.model.FromDefinition.resolveEndpoint(FromDefinition.java:72)

at org.apache.camel.impl.DefaultRouteContext.getEndpoint(DefaultRouteContext.java:90)

at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:857)

at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:172)

... 18 more

 

3)java代码里templete.sendBody("direct://xxxx")必须要加第2个参数 如:templete.sendBody("direct://xxxx", data) 否则会出现异常:

Exception in thread "main" java.lang.IllegalArgumentException: defaultEndpoint must be specified

at org.apache.camel.util.ObjectHelper.notNull(ObjectHelper.java:294)

at org.apache.camel.impl.DefaultProducerTemplate.getMandatoryDefaultEndpoint(DefaultProducerTemplate.java:445)

at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:342)

at com.kayak.itmon.web.demo.camel.CamelStart.main(CamelStart.java:13)

 

4
5
分享到:
评论
6 楼 Everyday都不同 2015-08-27  
string2020 写道
Everyday都不同 写道
string2020 写道
direct 和 vm有什么区别?

他们是不同的通讯组件,在camel中代表不同的通讯协议。direct接到消息后不做任何处理直接路由到下一个节点;vm 接收到消息后先存入队列中,然后由监听线程取出消息送入下一个节点进行处理.


就是说,功能和效果一样,只是一个是同步,一个是异步。
是吗?

5 楼 string2020 2015-08-27  
Everyday都不同 写道
string2020 写道
direct 和 vm有什么区别?

他们是不同的通讯组件,在camel中代表不同的通讯协议。direct接到消息后不做任何处理直接路由到下一个节点;vm 接收到消息后先存入队列中,然后由监听线程取出消息送入下一个节点进行处理.


就是说,功能和效果一样,只是一个是同步,一个是异步。
是吗?
4 楼 Everyday都不同 2015-08-26  
string2020 写道
direct 和 vm有什么区别?

他们是不同的通讯组件,在camel中代表不同的通讯协议。direct接到消息后不做任何处理直接路由到下一个节点;vm 接收到消息后先存入队列中,然后由监听线程取出消息送入下一个节点进行处理.
3 楼 string2020 2015-08-26  
direct 和 vm有什么区别?
2 楼 Everyday都不同 2015-08-26  
comsci 写道


   通过修改代码来实现对不同流程的同步访问?  运行时? 实时的?

   在修改代码之前,用户的运行过程必须全部终止吗?

额,我这篇的意思是说在数据的源头“分发”给不同流程,同时配置文件必须配置相应的不同流程。并不是说在现有流程运行的情况下修改代码。不知道这样说是否解决了你的困惑。
1 楼 comsci 2015-08-26  


   通过修改代码来实现对不同流程的同步访问?  运行时? 实时的?

   在修改代码之前,用户的运行过程必须全部终止吗?

相关推荐

    CAMEL 呼叫流程和信令流程

    ### CAMEL呼叫流程与信令流程详解 #### 概述 CAMEL(Customised Applications for Mobile network Enhanced Logic)是一种用于移动网络的智能网技术,它为移动运营商提供了定制化的增值服务功能。CAMEL主要应用于...

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

    服务编排是指将多个独立的服务组合成一个工作流程。在Camel中,这可以通过使用DSL(Domain Specific Language)来完成,例如Java DSL或XML DSL。开发者可以定义数据如何在不同服务之间流动,控制流程中的错误处理和...

    Camel in action(camel实战)

    Apache Camel 是一个强大的 Java 框架,它使得开发者能够轻松地实现企业级集成模式。通过简洁而强大的领域特定语言(DSL),开发者可以像拼接乐高积木一样将集成逻辑无缝嵌入到应用程序中。Apache Camel 支持超过 80...

    亲测好用——Apache Camel简介以及使用场景.pptx.zip

    Apache Camel 是一个强大的开源框架,主要用于企业集成领域,它提供了模型化的方式来进行应用程序之间的数据交换。这个PPT可能涵盖了以下内容: 1. **Apache Camel 的核心概念** - **DSL (Domain Specific ...

    ApacheCamel-JDBC

    Apache Camel 是一个流行的开源集成框架,它允许开发者以声明式的方式定义路由和转换数据,使得在不同的软件组件之间实现通信变得更加简单。JDBC(Java Database Connectivity)是Java平台中的一个标准API,用于与...

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

    Apache Camel 是一个强大的开源框架,专门用于构建企业级应用程序中的集成解决方案。它提供了一种声明式的方式,使得开发者可以轻松地定义数据路由和处理规则,从而实现系统间的通信。这个"Apache Camel 开发使用...

    Camel_Camel3Camel6函数_

    Camel6函数则是扩展了三峰骆驼函数,增加了一个额外的峰,使其具有四个局部极值点,增加了寻找全局最小值的难度。其数学形式可能为: ``` f(x) = a * (x^2 - b*x)^4 + c * (x^2 - d*x)^2 + e ``` 这里,`a`, `b`, ...

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

    - **定义**:Apache Camel 是一个强大的开源框架,它使开发者能够轻松地实现企业级集成模式(Enterprise Integration Patterns, EIP)。该框架提供了简洁而强大的领域特定语言(Domain-Specific Language, DSL),...

    Camel实战中文版第四章.pdf

    Camel支持单个参数绑定和多个参数绑定。单个参数绑定更简单直观,而多个参数绑定则适用于更复杂的情况。 - **单个参数绑定**: Camel自动将交换中的消息体绑定到bean方法的参数上。 - **多个参数绑定**: 当bean方法...

    camel in action 中文版 第一章

    Camel 打破了传统模式,它支持 80 多种不同的协议和数据类型,它的扩展性和模块性允许你实现你自己专有协议的无缝插件。 1.2.1 为什么使用 Camel Camel 为整합领域介绍了一些新奇的观点,这就是为什么它的作者们...

    Camel in Action

    重点介绍了Camel的核心特性之一——路由机制。详细解释了如何定义消息路由规则,并通过实例展示了如何构建复杂的路由网络。 **第二部分:Camel核心功能** - **第3章:使用Camel进行数据转换(Transforming data ...

    Apache Camel 集成组件.rar

    这在需要从多个数据库源聚合数据,或者基于查询结果触发流程的场景中非常实用。 **使用场景与优势** Apache Camel 的这些组件覆盖了广泛的应用场景,例如: - 数据交换:通过 HTTP 或 FTP 在系统之间传输文件。 -...

    Camel.in.Action

    3. **灵活且可扩展**:用户可以轻松地扩展Camel的功能,以适应各种不同的应用场景。 4. **广泛的组件支持**:Camel内置了大量组件,可以与各种不同的技术栈进行无缝集成。 5. **强大的社区支持**:活跃的社区确保了...

    apache-camel-3.7.0_数据同步_

    Apache Camel 是一个强大的开源框架,专门用于构建企业级集成解决方案。在标题“apache-camel-3.7.0_数据同步_”中提到的“数据同步”,是指利用Apache Camel实现不同系统、数据库或应用程序间的数据交换和一致性...

    camel文档

    - **扩展策略**:讨论如何根据业务需求来扩展Camel应用,以支持更大的负载。 - **第11章:开发Camel项目** - **项目结构**:介绍如何组织Camel项目的结构,以便于维护和扩展。 - **开发工具**:推荐一些常用的...

    Apache Camel 开发指南.rar

    Apache Camel 是一个强大的开源企业级集成框架,它简化了在Java应用程序之间建立复杂的消息传递流程。这个"Apache Camel 开发指南"压缩包包含了丰富的资源,帮助开发者深入理解Camel的各个方面,包括路由表达式、...

    apache camel 集成组件 教程

    4. **可扩展性:** 开发者可以根据需要扩展 Camel 的功能,比如创建自定义的组件或处理器。 5. **异步处理:** Camel 支持非阻塞的消息处理模式,有助于提高系统的吞吐量和响应速度。 6. **容错与重试机制:** Camel...

    Camel in action PDF和源代码

    《Camel in Action》是关于Apache Camel这一企业级集成框架的专业书籍,这本书深入浅出地讲解了如何使用Camel构建高效、可维护的集成解决方案。PDF版本提供了方便的电子阅读体验,而源代码则帮助读者更好地理解书中...

    Camel_应用开发文档.pdf

    * 微服务架构:Camel 能够帮助构建微服务架构,例如将多个微服务集成到一个系统中。 * IoT 应用:Camel 能够帮助构建 IoT 应用,例如将 sensor 数据从一个系统传输到另一个系统。 7. Camel 依赖 Camel 依赖是指在...

    ApacheCamel-FTP

    Apache Camel 是一个流行的开源集成框架,它允许开发者在不同的系统、协议和服务之间建立灵活的数据通信。FTP(File Transfer Protocol)是互联网上广泛使用的文件传输协议,用于上传、下载和管理远程服务器上的文件...

Global site tag (gtag.js) - Google Analytics