Using Camel Routes in Java EE components

Source: Internet
Author: User
Tags jboss wildfly apache camel

Summary: You can start using Apache camel Routes in Java EE components by integrating camel and WildFly application servers (using the Wildfly-camel subsystem).

"Editor's note" the author Markus Eisele is Red Hat's Developer advocate, mainly engaged in JBoss middleware related research, with more than 14 years of Java EE work experience. In this post, Markus mainly shares the Camel Routes application practice based on Java EE components.

The following are the translations:

Using Camel in a production environment for a while, I became more and more fond of its simplicity. There are some challenges to using it on Java EE, and in a recent speech I mentioned how to do that. In Java EE, we can use Camel in different ways, and it is recommended to use the Wildfly-camel subsystem. In the next series, I will explore the different ways to implement it and provide some examples that are not covered in the speech. I'm looking forward to receiving your feedback and asking questions via a message or a @myfear on Twitter.

Camel on WildFly 8.2 Getting Started

The Wildfly-camel subsystem provides an integrated environment for Apache Camel and WildFly application servers. It allows you to add Camel Routes (routes) as part of the WildFly configuration. Routes can be deployed as part of a Java EE application. Java EE components can use camel's core API and multiple camel Component APIs. Your enterprise-class integration solution can be architected on top of the federated capabilities of Java EE and Camel.

Note: The latest WildFly9 is expected to be supported by the 3.x version of Wildfly-camel.

Preparation phase

Download and unzip the WildFly 8.2.0.Final to the directory you specified, download and unzip the Wildfly-camel patch (2.3.0) to the WildFly directory. Start WildFly with the following command:

bin/standalone[.bat|.sh] -c standalone-camel.xml

The quickest way to start and run is to use Docker and WildFly Camel image. The image here requires a pre-installed WindFly8.1 and Camel subsystem.

Define and use Camel Context

The camelcontext represents a Camel routing rule library. The way to use Camelcontext and Spring ApplicationContext is very similar. It contains all the routes for your app. You can use as many camelcontext as you want, and of course they need to be defined with different names.

Wildfly-camel can be defined and deployed in the following 3 different ways:

    1. Defined as part of a subsystem in standalone-camel.xml and Domain.xml;
    2. In a support for their deployment artifact deployment, of course, this deployment artifact must contain the suffix-camel-context.xml file;
    3. Define the deployment and provide routing information through routebilder and CDI integration.

A defined camelcontext can be used in two different ways:

    1. Injected via CAMEL-CDI
    2. Enter through the JNDI tree.
Context and Route examples

In the following example, I will use the context of an associated route, provided through CDI and Routebuilder. is an application-level bean that starts automatically when the app starts. @ContextName annotations give Camelcontext a specific name.

@ApplicationScoped@Startup@ContextName("cdi-context")public class HelloRouteBuilder extends RouteBuilder {    @Inject    HelloBean helloBean;    @Override    public void configure() throws Exception {        from("direct:start").transform(body().prepend(helloBean.sayHello()).append(" user."));    } }

The route itself is not really challenging. It has an empty information body from Direct:start and uses the CDI bean method "SayHello" prepends output, and then append the string "user." As a reference, the complete code can be found in my GitHub (Https://github.com/myfear/camel-javaee). So what we need to know next is how to use this route in various Java EE components.

Using Camel in CDI

Camel supports CDI since version 2.10. Before a subsystem, it needs to be bootstrapped. But you don't need it now, you can simply inject its name into the @Named CDI bean with a deployed or defined camelcontext in a simple @Injecting.

@Inject    @ContextName("cdi-context")    private CamelContext context;
Using Camel in JSF, Jax-rs, and EJBs

With an understanding of how to use Camelcontext in CDI, you might think that using the same simplicity as in JSF is not the case-you can't inject it into Managedbeans or the CDI Beans that binds to the JSF component. In addition, it is not available in EJBs. I didn't dig into the details here, but I think it really needs to be improved in border control. A reasonable solution, in fact, a better application design is to put a complete Camel logic into a separate CDI bean and inject it.

@Namedpublic class HelloCamel {    @Inject    @ContextName("cdi-context")    private CamelContext context;    private final static Logger LOGGER = Logger.getLogger(HelloCamel.class.getName());    public String doSomeWorkFor(String name) {        ProducerTemplate producer = context.createProducerTemplate();        String result = producer.requestBody("direct:start", name, String.class);        LOGGER.log(Level.INFO, result);        return result;    }}

The Producertemplate interface allows you to send information exchanges from Java code to endpoint, making it easy to collaborate with Camel endpoint instances in a number of different ways. In this particular case, it simply starts the route and adds a string that represents me using the component name into the body.

The CDI Bean plays a backing-bean role for the components that use it:

@Inject    HelloCamel helloCamel;    public String getName() {        return helloCamel.doSomeWorkFor("JSF");}

The returned string is "Hello JSF user.", also written into the WildFly server log. This approach is also best for other Java EE components.

Using Camel in EJBs

If you are using EJBS as your main application component module, it is also reasonable to use the Jndi method:

CamelContext camelctx =              (CamelContext) inicxt.lookup("java:jboss/camel/context/cdi-context");
Hawtio: A Camel console

Another hidden treasure in the subsystem is the Hawtio console. This is a modular Web console for managing your Java components, which has an Apache Camel plug-in to visualize your context and routing information. Remember, it is automatically configured and, for security reasons, you need to add an administrative user before you can use it.

Original address: Using Camel Routes in Java EE components

This article is compiled and collated by OneAPM engineers. OneAPM is an emerging leader in application performance management, enabling enterprise users and developers to easily implement slow program code and real-time crawling of SQL statements. To read more technical articles, please visit the OneAPM official blog.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Using Camel Routes in Java EE components

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.