...
Tip |
---|
Favor using Netty as Netty is a much more active maintained and popular project than Apache Mina currently is |
Info |
---|
Be careful with sync=false on consumer endpoints. Since camel-mina2 all consumer exchanges are InOut. This is different to camel-mina. |
Maven users will need to add the following dependency to their pom.xml
for this component:
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mina2</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
|
URI format
Code Block |
---|
mina2:tcp://hostname[:port][?options]
mina2:udp://hostname[:port][?options]
mina2:vm://hostname[:port][?options]
|
...
Div | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Using a custom codec
See the Mina how to write your own codec. To use your custom codec with camel-mina
, you should register your codec in the Registry; for example, by creating a bean in the Spring XML file. Then use the codec
option to specify the bean ID of your codec. See HL7 that has a custom codec.
...
In this sample, Camel exposes a service that listens for TCP connections on port 6200. We use the textline codec. In our route, we create a Mina consumer endpoint that listens on port 6200:
Code Block |
---|
from("mina2:tcp://localhost:" + port1 + "?textline=true&sync=false").to("mock:result");
|
As the sample is part of a unit test, we test it by sending some data to it on port 6200.
Code Block |
---|
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("Hello World");
template.sendBody("mina2:tcp://localhost:" + port1 + "?textline=true&sync=false", "Hello World");
assertMockEndpointsSatisfied();
|
...
In the next sample, we have a more common use case where we expose a TCP service on port 6201 also use the textline codec. However, this time we want to return a response, so we set the sync
option to true
on the consumer.
Code Block |
---|
from("mina2:tcp://localhost:" + port2 + "?textline=true&sync=true").process(new Processor() {
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
exchange.getOut().setBody("Bye " + body);
}
});
|
Then we test the sample by sending some data and retrieving the response using the template.requestBody()
method. As we know the response is a String
, we cast it to String
and can assert that the response is, in fact, something we have dynamically set in our processor code logic.
Code Block |
---|
String response = (String)template.requestBody("mina2:tcp://localhost:" + port2 + "?textline=true&sync=true", "World");
assertEquals("Bye World", response);
|
...
Spring DSL can, of course, also be used for Mina MINA. In the sample below we expose a TCP server on port 5555:
Code Block | ||||
---|---|---|---|---|
| ||||
<route>
<from uri="mina2:tcp://localhost:5555?textline=true"/>
<to uri="bean:myTCPOrderHandler"/>
</route>
|
In the route above, we expose a TCP server on port 5555 using the textline codec. We let the Spring bean with ID, myTCPOrderHandler
, handle the request and return a reply. For instance, the handler bean could be implemented as follows:
Code Block | ||||
---|---|---|---|---|
| ||||
public String handleOrder(String payload) {
...
return "Order: OK"
}
|
...
For instance, the example below will close the session after it has written the bye
message back to the client:
Code Block | ||||
---|---|---|---|---|
| ||||
from("mina2:tcp://localhost:8080?sync=true&textline=true").process(new Processor() {
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
exchange.getOut().setBody("Bye " + body);
exchange.getOut().setHeader(Mina2Constants.MINA_CLOSE_SESSION_WHEN_COMPLETE, true);
}
});
|
...
Filters permit you to use some Mina Filters, such as SslFilter
. You can also implement some customized filters. Please note that codec
and logger
are also implemented as Mina filters of type, IoFilter
. Any filters you may define are appended to the end of the filter chain; that is, after codec
and logger
.
Include Page | ||||
---|---|---|---|---|
|