如何在同一场战争的多个 jar 中使用相同的 CamelContext [英] How to use same CamelContext in multiple jar on the same war

查看:18
本文介绍了如何在同一场战争的多个 jar 中使用相同的 CamelContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是camel 2.16.2,我需要在多个jar 中使用一个CamelContext,因为我需要将所有Camel Routers 放入一个CamelContext.所以我的战争将把所有这些罐子都当作 maven 神器.

I'm using the camel 2.16.2 and I need to use the one CamelContext across multiple jars as I need to have all the Camel Routers in to one CamelContext. So my war will have all those jars as maven artifacts.

请告诉我如何处理上述情况?

Please let me know how do I handle above scenario?

只是为了详细说明上述问题.在我的战争 myApp.war 中,我已经初始化了 CamelContext.共有三个 jar 文件 myApp1.jar、myApp2.jar 和 myApp3.jar.每个 jar 都有自己单独定义的路由器.

Just to elaborate more on above question. In my war myApp.war, I have initialized the CamelContext. There are three jars myApp1.jar, myApp2.jar and myApp3.jar. Each jar has it own routers defined separately.

  1. 如何在每个 jar 中启动路由器?
  2. 我可以使用注入到每个路由器的相同 CamelContext 吗?
  3. 如果我不能通过 jars 处理,是否可以使用多个战争(myApp1.war、myApp2.war 和 myApp3.war)来实现,每个战争都有不同的骆驼上下文,并从主战争(myApp.war)与这些路由器通信) ?

推荐答案

在做了一些研究之后找到了实现这一点的方法.事实上,我们可以在不同的 jar 中使用相同的 CamelContext,因为所有 jar 都在同一个战争(Web 容器)中.

After doing some research found a way to implement this. Infact we can use the same CamelContext across different jars as all jars are in the same war (Web Container).

我们可以使用带有骆驼 CDI 的 Apache Camel 2.16.2 轻松实现.如果您使用wildfly 来部署您的war,那么您可能需要添加camel 补丁.下载 wildfly 9.0.2 pach

We can implement easily with Apache Camel 2.16.2 with camel CDI. If you're using wildfly to deploy your war then you may need to add the camel patch. Download the the wildfly 9.0.2 pach

下面给出了步骤.

在你的war中创建一个servlet或restService并注入camelContext.

In your war create a servlet or restService and Inject the camelContext.

@Inject
@ContextName("cdi-context")
private CamelContext camelctx;

使用以下注释在 jar 中创建一个路由器.

Create a router in the jar with below annotation.

@Startup
@ApplicationScoped
public class MyJRouteBuilder extends RouteBuilder {

在配置方法中添加

@Override
public void configure() throws Exception {
    from("direct:startTwo").routeId("MyJRouteBuilder")
    .bean(new SomeBeanThree());
}

在你的 jar 中创建一个 BootStrap 类并添加路由器

Create a BootStrap Class in your jar and add the Router

@Singleton
@Startup
public class BootStrap {

private CamelContext camelctx;

@PostConstruct
public void init() throws Exception {   
    camelctx.addRoutes(new MyJRouteBuilder());
}

将您的 jar 作为工件添加到 war pom.xml 中.部署战争后,您可以看到 MyJRouteBuilder 已在 cdi-context CamelContext 中注册.所以现在您可以随时随地访问您的路由器.

Add your jar as a artifact in the war pom.xml. Once you deploy the war you can see MyJRouteBuilder is Registred in the cdi-context CamelContext. So now you can access your Router anywhere you want.

希望这对与我有同样问题的人有用.

Hope this would useful anyone who has the same issue what I had.

这篇关于如何在同一场战争的多个 jar 中使用相同的 CamelContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆