|
Let’s first look at the budget car and luxury car insurance endpoints in listing 10. Note that this listing consists of a JMS BC configuration and a File BC configuration. The JMS BC configuration is actually part of the same xbean.xml file as shown in listing 7 as you can see in the article’s source code.
Listing 10 The JMS BC and File BC configurations for the car insurance endpoints.
<!-- JMS BC SU configuration -->
<jms rovider service="esb:luxuryCarSender"
endpoint="jmsEndpoint"
destinationName="luxurycar.send"
connectionFactory="#connectionFactory"/>
<!-- File BC SU configuration -->
<beans xmlns:file="http://servicemix.apache.org/file/1.0"
xmlns:esb="http://esbinaction.com/insurance">
<classpath>
<location>.</location>
<location>bcel.jar</location>
<location>jibx-bind.jar</location>
<location>jibx-extras.jar</location>
<location>jibx-run.jar</location>
<location>qdox-1.6.1.jar</location>
<location>stax-api.jar</location>
<location>wstx-asl.jar</location>
<location>xmlpull_1_1_4.jar</location>
<location>xpp3.jar</location>
</classpath>
<file:sender service="esb:budgetCarSender"
endpoint="fileEndpoint"
directory="file:budgetCarIn"
marshaler="#InsuranceFileMarshaler"/>
<bean id="InsuranceFileMarshaler"
class="esb.dzone.servicemix.util.InsuranceFileMarshaler"/>
</beans>
The JMS configuration is pretty standard as it just sends the message to a JMS queue named luxury.send. The File BC configuration defines a file sender, which sends the message from the content-based router to a file directory named budgetCarIn. But, because we want the message to be formatted as a CSV file, we have defined another marshaler here. The file marshaler uses JiBX to transform the XML message to a CarInsuranceRequest Java object, which is transformed to a CSV message with similar transformation logic as the Mule example implementation of the previous article. Let’s look at the file marshaler in listing 11.
Listing 11 The file marshaler implementation.
public class InsuranceFileMarshaler extends DefaultFileMarshaler {
protected void writeMessageContent(MessageExchange exchange,
NormalizedMessage message, OutputStream out, String path)
throws MessagingException {
Source src = message.getContent();
if (src == null) {
throw new NoMessageContentAvailableException(exchange);
}
try {
CarInsuranceRequest request = (CarInsuranceRequest)
JiBXUtil.unmarshalDocument(src, CarInsuranceRequest.class);
String csvMessage = getInsuranceCSV(request);
OutputStreamWriter writer = new OutputStreamWriter(out);
writer.write(csvMessage);
writer.flush();
} catch (Exception e) {
throw new MessagingException(e);
}
}
private String getInsuranceCSV(CarInsuranceRequest request) {
return new StringBuffer()
.append(request.getRequestID())
.append(","
.append(request.getNumberPlate())
.append(","
.append(request.getCarType())
.append(","
.append(request.getBuildYear())
.append(","
.append(new SimpleDateFormat().format(request.getStartDate()))
.toString();
}
}
The file marshaler just transforms the CarInsuranceRequest to a CSV message and writes it to a file with an OutputStreamWriter. This completes the car insurance request implementation, so now let’s move on to the travel insurance request handling.
Before we can send the travel insurance request to the webservice, we first have to transform the XML to the target format defined by the webservice WSDL. To reduce the lines of code in this article a bit (there’s enough code already to my opinion), we will focus on the ServiceMix configuration. Because the ServiceMix Saxon SE, which provides transformation functionality based on XSLT, requires an in-out MEP, we first have to transform the message exchange to in-out with an EIP pipeline as already explained in figure 4. And because the CXF BC, which provides webservice functionality, also requires an in-out MEP we need to define two pipelines as shown in listing 12.
Listing 12 The EIP pipeline definitions to be able to invoke the travel webservice.
<beans xmlns:eip="http://servicemix.apache.org/eip/1.0"
xmlns:esb="http://esbinaction.com/insurance"
xmlns:tri="http://dzone.com/travelInsurance">
<eip ipeline service="esb:travelPipeline" endpoint="routingEndpoint">
<eip:transformer>
<eip:exchange-target service="esb:transformTravelRequest"/>
</eip:transformer>
<eip:target>
<eip:exchange-target service="esb:travelServicePipeline" />
</eip:target>
</eip ipeline>
<eip ipeline service="esb:travelServicePipeline" endpoint="routingEndpoint">
<eip:transformer>
<eip:exchange-target service="tri:TravelInsuranceServiceImplService"
operation="tri:getTravelInsurance"/>
</eip:transformer>
<eip:target>
<eip:exchange-target service="esb:insuranceSender" />
</eip:target>
</eip ipeline>
</beans>
The first pipeline definition is invoked from the Camel content-based router implementation shown in listing 9. In the first pipeline we invoke the Saxon SE, which transforms the XML message to the webservice message format. Then the first pipeline invokes the second pipeline to invoke the webservice with an in-out MEP. So let’s quickly look at the Saxon SE configuration in listing 13.
Listing 13 The Saxon SE configuration, which transforms the XML message to the travel web service message format.
<beans xmlns:saxon="http://servicemix.apache.org/saxon/1.0"
xmlns:esb="http://esbinaction.com/insurance">
<saxon:xslt service="esb:transformTravelRequest" endpoint="xsltEndpoint"
resource="classpath:TravelRequest.xslt" />
</beans> |
|