...
MyEndpoint.java
- ExtendsEndpoint
and implementsExchangeProcessor
.Endpoint
provides a referenceable resource for the component and theExchangeProcessor
provides the ability for the JBI container to process a message exchange with the component. TheExchangeProcessor.process()
method is where we will place make a call to a method containing the logic to print out the Hello World message.
Adding the Logic to Print the Message
When creating a JBI component, how a message exchange is handled depends on whether a component is a consumer or a provider. Because the Hello World SE will not be consuming any other service (i.e., sending a message to another service), it is a provider (i.e., it will only be providing its service via a return message). As mentioned above, the ExchangeProcessor.process()
method is of interest because it is where the message exchange is handled, so let's examine this method:
Code Block |
---|
public void process(MessageExchange exchange) throws Exception {
// The component acts as a provider, this means that another component has requested our service
// As this exchange is active, this is either an in or a fault (out are send by this component)
if (exchange.getRole() == MessageExchange.Role.PROVIDER) {
// Check here if the mep is supported by this component
if (exchange instanceof InOut == false) {
throw new UnsupportedOperationException("Unsupported MEP: " + exchange.getPattern());
}
// In message
if (exchange.getMessage("in") != null) {
NormalizedMessage in = exchange.getMessage("in");
// TODO ... handle the in message
// If the MEP is an InOnly, RobustInOnly, you have to set the exchange to DONE status
// else, you have to create an Out message and populate it
// For now, just echo back
NormalizedMessage out = exchange.createMessage();
out.setContent(in.getContent());
exchange.setMessage(out, "out");
channel.send(exchange);
// Fault message
} else if (exchange.getFault() != null) {
// TODO ... handle the fault
exchange.setStatus(ExchangeStatus.DONE);
channel.send(exchange);
// This is not compliant with the default MEPs
} else {
throw new IllegalStateException("Provider exchange is ACTIVE, but no in or fault is provided");
}
// The component acts as a consumer, this means this exchange is received because
// we sent it to another component. As it is active, this is either an out or a fault
// If this component does not create / send exchanges, you may just throw an UnsupportedOperationException
} else if (exchange.getRole() == MessageExchange.Role.CONSUMER) {
// Exchange is finished
if (exchange.getStatus() == ExchangeStatus.DONE) {
return;
// Exchange has been aborted with an exception
} else if (exchange.getStatus() == ExchangeStatus.ERROR) {
return;
// Exchange is active
} else {
// Out message
if (exchange.getMessage("out") != null) {
// TODO ... handle the response
exchange.setStatus(ExchangeStatus.DONE);
channel.send(exchange);
// Fault message
} else if (exchange.getFault() != null) {
// TODO ... handle the fault
exchange.setStatus(ExchangeStatus.DONE);
channel.send(exchange);
// This is not compliant with the default MEPs
} else {
throw new IllegalStateException("Consumer exchange is ACTIVE, but no out or fault is provided");
}
}
// Unknown role
} else {
throw new IllegalStateException("Unkown role: " + exchange.getRole());
}
}
|
The implementation of this method was provided by the servicemix-service-engine Maven archetype and is shown in the code snippet above. Because the archetype can be used to create a consumer or a provider, this snippet contains a conditional block for handling either a consuemr or a provider role. In the case of the Hello World SE, we are only interested in the provider role so we're going to make some changes to MyEndpoint
.
Instead of extending the Endpoint
class, we're going to extend a different class that is specifically for providers named ProviderEndpoint
. This class provides some additional conveniences for providers and will make the job of implementing MyEndpoint
as a provider much easier.
...
Creating the Maven Subprojects For the Service Unit and Service Assembly
...