Chapter 5. JavaEE Integration
This chapter provides the necessary information about the integration points with JavaEE.
5.1. CDI リンクのコピーリンクがクリップボードにコピーされました!
The Camel CDI component provides an auto-configuration for Apache Camel, using CDI as dependency injection framework. However, it is based on convention-over-configuration. It implements the standard camel bean integration so that you can use the Camel annotations easily in CDI beans.
For more information about CDI, refer to the cdi documentation.
The following example describes how you can consume and assosciate the Camel Context with a route.
@Inject @ContextName("cdi-context") private CamelContext camelctx;
@Inject
@ContextName("cdi-context")
private CamelContext camelctx;
5.1.1. Importing XML DSL configuration リンクのコピーリンクがクリップボードにコピーされました!
Camel CDI integration enables you to import existing XML DSL files via the @ImportResource
annotation:
@ImportResource("camel-context.xml") class MyBean { }
@ImportResource("camel-context.xml")
class MyBean {
}
The location of the imported file must be present on the deployment classpath. Placing the file into locations such as WEB-INF
will not work. However, WEB-INF/classes
will work fine.
5.2. EJB リンクのコピーリンクがクリップボードにコピーされました!
Management support is provided through the ejb component which integrates with the EJB3 subsystem.
5.3. JAXB リンクのコピーリンクがクリップボードにコピーされました!
JAXB support is provided through the Camel JAXB data format.
Camel supports unmarshalling XML data to JAXB annotated classes and marshalling from classes to XML. The following demonstrates a simple Camel route for marshalling and unmarshalling with the Camel JAXB data format class.
5.3.1. JAXB Annotated class リンクのコピーリンクがクリップボードにコピーされました!
5.3.2. JAXB Class XML representation リンクのコピーリンクがクリップボードにコピーされました!
<customer xmlns="http://org/wildfly/test/jaxb/model/Customer"> <firstName>John</firstName> <lastName>Doe</lastName> </customer>
<customer xmlns="http://org/wildfly/test/jaxb/model/Customer">
<firstName>John</firstName>
<lastName>Doe</lastName>
</customer>
5.3.3. Camel JAXB Unmarshalling リンクのコピーリンクがクリップボードにコピーされました!
5.3.4. Camel JAXB Marshalling リンクのコピーリンクがクリップボードにコピーされました!
5.4. JAX-RS リンクのコピーリンクがクリップボードにコピーされました!
JAX-RS support is provided by Camel CXF-RS.
5.4.1. CXF-RS Producer リンクのコピーリンクがクリップボードにコピーされました!
5.4.2. CXF-RS Consumer リンクのコピーリンクがクリップボードにコピーされました!
5.4.3. JAX-RS Consumer with the Camel REST DSL リンクのコピーリンクがクリップボードにコピーされました!
The Camel REST DSL gives the capability to write Camel routes that act as JAX-RS consumers. The following RouteBuilder class demonstrates this.
By setting the binding mode, Camel can marshal and unmarshal JSON data either by specifying a 'produces()' or 'type()' configuration step.
-
The REST DSL configuration starts with
restConfiguration().component("undertow")
. - The Camel on EAP Subsystem only supports the camel-servlet and camel-undertow components for use with the REST DSL. However, it does not work if you configure the other components.
5.4.4. Security リンクのコピーリンクがクリップボードにコピーされました!
Refer to the JAX-RS security section.
5.4.5. Quickstart examples in Fuse on EAP リンクのコピーリンクがクリップボードにコピーされました!
A quickstart example is available in your Fuse on EAP installation at quickstarts/camel/camel-cxf-jaxrs
directory.
5.5. JAX-WS リンクのコピーリンクがクリップボードにコピーされました!
WebService support is provided through the CXF component which integrates with the JBoss EAP WebServices subsystem that also uses Apache CXF.
5.5.1. JAX-WS CXF Producer リンクのコピーリンクがクリップボードにコピーされました!
The following code example uses CXF to consume a web service which has been deployed by the WildFly web services subsystem.
5.5.1.1. JAX-WS web service リンクのコピーリンクがクリップボードにコピーされました!
The following simple web service has a simple 'greet' method which will concatenate two string arguments together and return them.
When the JBoss EAP web service subsystem detects classes containing JAX-WS annotations, it bootstraps a CXF endpoint. In this example the service endpoint will be located at http://hostname:port/context-root/greeting.
5.5.1.2. Camel route configuration リンクのコピーリンクがクリップボードにコピーされました!
This RouteBuilder configures a CXF producer endpoint which will consume the 'greeting' web service defined above. CDI in conjunction with the camel-cdi component is used to bootstrap the RouteBuilder and CamelContext.
The greeting web service 'greet' requires two parameters. These can be supplied to the above route by way of a ProducerTemplate
. The web service method argument values are configured by constructing an object array which is passed as the exchange body.
5.5.2. Camel CXF JAX-WS Consumer リンクのコピーリンクがクリップボードにコピーされました!
5.5.3. Security リンクのコピーリンクがクリップボードにコピーされました!
Refer to the JAX-WS security section.
5.5.4. Quickstart examples in Fuse on EAP リンクのコピーリンクがクリップボードにコピーされました!
A quickstart example is available in your Fuse on EAP installation at quickstarts/camel/camel-cxf-jaxws
directory.
5.6. JMS リンクのコピーリンクがクリップボードにコピーされました!
Messaging support is provided through the JMS component which integrates with the JBoss EAP Messaging (ActiveMQ Artemis) subsystem.
Integration with other JMS implementations is possible through configuration of vendor specific resource adapters, or if not available, by using the JBoss Generic JMS resource adapter.
5.6.1. JBoss EAP JMS configuration リンクのコピーリンクがクリップボードにコピーされました!
You can configure the JBoss EAP messaging subsystem through the standard JBoss EAP XML configuration files. For example, standalone.xml file.
For the examples, that follow you use the embedded ActiveMQ Artemis in memory instance. You first configure a new JMS queue on the messaging subsystem by adding the following XML configuration to the jms-destinations section.
<jms-queue name="WildFlyCamelQueue"> <entry name="java:/jms/queue/WildFlyCamelQueue"/> </jms-queue>
<jms-queue name="WildFlyCamelQueue">
<entry name="java:/jms/queue/WildFlyCamelQueue"/>
</jms-queue>
Alternatively you could use a CLI script to add the queue.
jms-queue add --queue-address=WildFlyCamelQueue --entries=queue/WildFlyCamelQueue,java:/jms/queue/WildFlyCamelQueue
$ jms-queue add --queue-address=WildFlyCamelQueue --entries=queue/WildFlyCamelQueue,java:/jms/queue/WildFlyCamelQueue
Also, you can create a messaging-deployment
configuration within a custom jms.xml deployment descriptor. See section 'Deployment of -jms.xml files' within the JBoss EAP messaging subsystem documentation for more information.
5.6.2. Camel route configuration リンクのコピーリンクがクリップボードにコピーされました!
The following JMS producer and consumer examples make use of JBoss EAP’s embedded ActiveMQ Artemis server to publish and consume messages to and from destinations.
The examples also use CDI in conjunction with the camel-cdi component. JMS ConnectionFactory instances are injected into the Camel RouteBuilder through JNDI lookups.
5.6.2.1. JMS Producer リンクのコピーリンクがクリップボードにコピーされました!
The DefaultJMSConnectionFactory
connection factory is injected into the RouteBuilder from JNDI. Under the JBoss EAP XML configuration, you can find the connection factory, within the messaging subsystem.
Next a timer endpoint runs every 10 seconds to send an XML payload to the WildFlyCamelQueue destination that has been configured earlier.
A log message will be output to the console each time a JMS message is added to the WildFlyCamelQueue destination. To verify that the messages really are being placed onto the queue, you can use the JBoss EAP administration console.
5.6.2.2. JMS Consumer リンクのコピーリンクがクリップボードにコピーされました!
To consume JMS messages the Camel RouteBuilder implementation is similar to the producer example.
As before, the connection factory is discovered from JNDI, injected and set on the JMSComponent instance.
When the JMS endpoint consumes messages from the WildFlyCamelQueue destination, the content is logged to the console.
5.6.2.3. JMS Transactions リンクのコピーリンクがクリップボードにコピーされました!
To enable Camel JMS routes to participate in JMS transactions, some additional configuration is required. Since camel-jms is built around spring-jms, you need to configure some Spring classes to enable them to work with JBoss EAP’s transaction manager and connection factory. The following code example demonstrates how to use CDI to configure a transactional JMS Camel route.
The camel-jms component requires a transaction manager of type org.springframework.transaction.PlatformTransactionManager
. Therefore, you start by creating a bean extending JtaTransactionManager
. Note that the bean is annotated with @Named
to allow the bean to be registered within the Camel bean registry. Also note that the JBoss EAP transaction manager and user transaction instances are injected using CDI.
Next, you need to declare the transaction policy that you want to use. Again, use the @Named
annotation to make the bean available to Camel. The transaction manager is also injected so that a TransactionTemplate can be created with the desired transaction policy. PROPAGATION_REQUIRED in this instance.
Now you can configure our Camel RouteBuilder class and inject the dependencies needed for the Camel JMS component. The JBoss EAP XA connection factory is injected together with the transaction manager that has been configured earlier.
In this example RouteBuilder, whenever any messages are consumed from queue1, they are routed to another JMS queue named queue2. Messages consumed from queue2 result in JMS transaction being rolled back using the rollback() DSL method. This results in the original message being placed onto the dead letter queue(DLQ).
5.6.2.4. Remote JMS destinations リンクのコピーリンクがクリップボードにコピーされました!
It is possible for one JBoss EAP instance to send messages to ActiveMQ Artemis destinations configured on another JBoss EAP instance through remote JNDI.
Some additional JBoss EAP configuration is required to achieve this. First an exported JMS queue is configured.
Only JNDI names bound in the java:jboss/exported
namespace are considered as candidates for remote clients, so the queue is named appropriately.
You must configure the queue on the JBoss EAP client application server andJBoss EAP remote server.
<jms-queue name="RemoteQueue"> <entry name="java:jboss/exported/jms/queues/RemoteQueue"/> </jms-queue>
<jms-queue name="RemoteQueue">
<entry name="java:jboss/exported/jms/queues/RemoteQueue"/>
</jms-queue>
Before the client can connect to the remote server, user access credentials need to be configured. On the remote server run the add user utility to create a new application user within the 'guest' group. This example has a user with the name 'admin' and a password of 'secret'.
The RouteBuilder implementation is different to the previous examples. Instead of injecting the connection factory, you need to configure an InitialContext and retrieve it from JNDI ourselves.
The configureInitialContext
method creates this InitialContext. Notice that you need to set a provider URL which should reference your remote JBoss EAP instance host name and port number. This example uses the JBoss EAP JMS http-connector, but there are alternatives documented here.
Finally the route is configured to send an XML payload every 10 seconds to the remote destination configured earlier - 'RemoteQueue'.
5.6.3. Security リンクのコピーリンクがクリップボードにコピーされました!
Refer to the JMS security section.
5.6.4. Quickstart examples in Fuse on EAP リンクのコピーリンクがクリップボードにコピーされました!
A quickstart example is available in your Fuse on EAP installation at quickstarts/camel/camel-jms
directory.
5.7. JMX リンクのコピーリンクがクリップボードにコピーされました!
You can provide management support through the JMX component which integrates with the JBoss EAP JMX subsystem.
5.8. JNDI リンクのコピーリンクがクリップボードにコピーされました!
JNDI integration is provided by a JBoss EAP specific CamelContext as shown below:
InitialContext inictx = new InitialContext(); CamelContextFactory factory = inictx.lookup("java:jboss/camel/CamelContextFactory"); WildFlyCamelContext camelctx = factory.createCamelContext();
InitialContext inictx = new InitialContext();
CamelContextFactory factory = inictx.lookup("java:jboss/camel/CamelContextFactory");
WildFlyCamelContext camelctx = factory.createCamelContext();
From a WildFlyCamelContext
you can obtain a preconfigured Naming Context
Context context = camelctx.getNamingContext(); context.bind("helloBean", new HelloBean());
Context context = camelctx.getNamingContext();
context.bind("helloBean", new HelloBean());
which can then be referenced from Camel routes.
5.9. JPA リンクのコピーリンクがクリップボードにコピーされました!
JPA integration is provided by the Camel JPA component. You can develop Camel JPA applications by providing a persistence.xml configuration file together with some JPA annotated classes.
5.9.1. Example persistence.xml リンクのコピーリンクがクリップボードにコピーされました!
In the following example, you can use the JBoss EAP in-memory ExampleDS datasource which is configured within the JBoss EAP standalone.xml configuration file.
5.9.2. Example JPA entitiy リンクのコピーリンクがクリップボードにコピーされました!
5.9.3. Camel JPA endpoint / route configuration リンクのコピーリンクがクリップボードにコピーされました!
Having configured JPA, you can make use of CDI to inject an EntityManager and UserTransaction instance into your RouteBuilder class or test case:
@PersistenceContext EntityManager em; @Inject UserTransaction userTransaction;
@PersistenceContext
EntityManager em;
@Inject
UserTransaction userTransaction;
Now to configure the Camel routes and JPA endpoint:
Finally, you can send a 'Customer' entity to the 'direct:start' endpoint and then query the ExampleDS datasource to verify that a record was saved.