The nmr component is an adapter to the Normalized Message Router (NMR) in ServiceMix, which is intended for use by Apache Camel applications deployed directly into the OSGi container. By contrast, the JBI component is intended for use by Apache Camel applications deployed into the ServiceMix JBI container.
By contrast, the JBI component is intended for use by Camel applications deployed into the ServiceMix JBI container.
The NMR component is provided with Apache ServiceMix. It is not distributed with Apache Camel. To install the NMR component in ServiceMix, enter the following command in the ServiceMix console window:
features install nmr
In plain Karaf the nmr component can also be installed using the servicemix artifacts:
features:chooseurl camel <version> features:addurl mvn:org.apache.servicemix.nmr/apache-servicemix-nmr/1.5.0/xml/features features:install camel-blueprint nmr camel-nmr install -s mvn:org.apache.servicemix.camel/org.apache.servicemix.camel.component/4.4.2
You also need to instantiate the NMR component. You can do this by
editing your Spring configuration file,
META-INF/spring/*.xml
, and adding the
following bean
instance:
<beans xmlns:osgi="http://www.springframework.org/schema/osgi" ... > ... <bean id="nmr" class="org.apache.servicemix.camel.nmr.ServiceMixComponent"> <property name="nmr"> <osgi:reference interface="org.apache.servicemix.nmr.api.NMR" /> </property> </bean> ... </beans>
The following code:
from("nmr:MyServiceEndpoint")
Automatically exposes a new endpoint to the bus with endpoint name
MyServiceEndpoint
(see URI format).
When an NMR endpoint appears at the end of a route, for example:
to("nmr:MyServiceEndpoint")
the messages sent by this producer endpoint are sent to the already deployed NMR endpoint.
An NMR endpoint supports these options:
Option | Default | Description |
---|---|---|
synchronous
|
false
|
When set to true on a consumer endpoint, an incoming,
synchronous NMR Exchange is handled on the sender's thread, instead of on a new
thread from the NMR endpoint's thread pool. |
runAsSubject
|
false
|
Apache ServiceMix 4.4: When set to
true on a consumer endpoint, the endpoint is invoked on
behalf of the Subject and set on the
Exchange (that is, the call to
Subject.getSubject(AccessControlContext) returns the
Subject instance). |
timeout
|
0
|
Apache ServiceMix 4.4: When set to a value
greater than 0 , the producer endpoint times out if it
doesn't receive a response from the NMR within the given timeout period (in
milliseconds). Configuring a timeout value switches from the default
asynchronous messaging to using synchronous interactions with the NMR. |
throwExceptionOnFailure
|
true | Apache ServiceMix 4.5.2: When set to
false , the NMR's exceptions (such as TimeoutException)
are consumed silently. |
Consumer:
// consume nmr exchanges asynchronously from("nmr:MyServiceEndpoint") // consume nmr exchanges synchronously and use the same thread as defined by NMR ThreadPool from("nmr:MyServiceEndpoint?synchronous=true").to()
Producer:
// produce nmr exchanges asynchronously from()...to("nmr:MyServiceEndpoint") // produce nmr exchanges synchronously and wait till 10s to receive response from()...to("nmr:MyServiceEndpoint?timeout=10000")
If you are using a stream type as the message body, be aware that a stream is capable
of being read only once. So if you enable DEBUG
logging, the body is
usually logged and thus read. To deal with this, Camel has a
streamCaching
option that can cache the stream, enabling you to
read it multiple times.
from("nmr:MyEndpoint").streamCaching().to("xslt:transform.xsl", "bean:doSomething");
From Apache Camel 1.5 onwards, stream caching is
enabled by default, so you need not set the streamCaching()
option.
In Apache Camel 2.0 big input streams (by default, over
64K) are stored in a temp
file, using
CachedOutputStream
. When you close the input stream, the temp
file is deleted.
NMR camel routes can be tested using the Apache Camel unit test approach, even if they
will be deployed next in different bundles in an OSGi runtime. With this aim in view,
you will extend the ServiceMixNMR
Mock class,
org.apache.servicemix.camel.nmr.AbstractComponentTest
, which will
create an NMR bus, register the Apache Camel NMR Component and the endpoints defined into
the Apache Camel routes.
public class ExchangeUsingNMRTest extends AbstractComponentTest { @Test public void testProcessing() throws InterruptedException { MockEndpoint mock = getMockEndpoint("mock:simple"); mock.expectedBodiesReceived("Simple message body"); template.sendBody("direct:simple", "Simple message body"); assertMockEndpointsSatisfied(); } @Override protected RouteBuilder createRouteBuilder() throws Exception { returnnew RouteBuilder() { @Override public void configure() throws Exception { from("direct:simple").to("nmr:simple"); from("nmr:simple?synchronous=true").to("mock:simple"); } }; } }