|
Open eHealth Integration Platform 2.x : First steps tutorial
This page last changed on Sep 28, 2009 by jens.riemschneider.
First steps tutorial
This tutorial is targeted at developers who want to get started with IPF and do not have a strong background on Camel and Groovy. The main goal is to have a very simple IPF application up and running very quickly. More technical background information is given in the First details tutorial.
Source codeThe source code for this tutorial can be downloaded from here. Project creationWe start by creating an IPF project via a Maven archetype. Navigate to a directory of your choice and create the project by entering the following on the command line (make sure to have the command on a single line): mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=org.openehealth.ipf.archetypes -DarchetypeArtifactId=ipf-archetype-basic -DarchetypeVersion=2.0.0 -DgroupId=org.openehealth.tutorial -DartifactId=basic -Dversion=1.0-SNAPSHOT -DinteractiveMode=false This will create a folder named basic in the current directory. Change to the basic folder and enter mvn install This will compile the project and install the project artifacts into your local Maven cache. To import the project into Eclipse navigate to File->Import->General->Existing Projects into Workspace in the Eclipse menu and select the created basic folder as root directory. After having imported the project into Eclipse it should look similar to the following figure (exact display might vary depending on your workspace settings).
In addition to an IPF application skeleton the archetype also created some simple example files that can be used for initial experiments. Route definitionSampleRouteBuilder.groovy defines two routes. A route is a logical unit of a sequence of message processing steps. The routes are implemented in the Groovy programming language, but we'll see that the syntax used is very close to Java. SampleRouteBuilder.groovy class SampleRouteBuilder extends SpringRouteBuilder { void configure() { from('direct:input1').transmogrify { it * 2 } // duplicate the request string e.g. 'abc' -> 'abcabc' from('direct:input2').reverse() // revert the request string e.g. 'abc' -> 'cba' } }
.transmogrify { it.reverse() }
For the moment we can treat direct endpoints as internal labels of the Camel routes, that can e.g. be used from JUnit tests like the following: SampleRouteTest.java
...
@ContextConfiguration(locations = { "/context.xml" })
@ContextConfiguration(locations = { "/context.xml" })
public class SampleRouteTest {
@Autowired
private ProducerTemplate producerTemplate;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testMultiply() throws Exception {
assertEquals("abcabc", producerTemplate.requestBody("direct:input1", "abc"));
}
@Test
public void testReverse() throws Exception {
assertEquals("cba", producerTemplate.requestBody("direct:input2", "abc"));
}
}
This test sends a message to each endpoint and expects the messages to be duplicated or reversed, respectively. Route extensionWe now extend the example to make our integration logic accessible from the outside.
In addition we want to keep the direct endpoints so that we can still test the message transformation with the JUnit test shown above. Instead of only returning the processing result to the HTTP client we also write the message to a file. Here's the extended route definition: SampleRouteBuilder.groovy class SampleRouteBuilder extends SpringRouteBuilder { void configure() { from('jetty:http://0.0.0.0:8080/tutorial') // receive client requests on http://0.0.0.0:8080/tutorial .convertBodyTo(String.class) // convert request input stream into a string .to('direct:input1') // continue from direct:input1 from('direct:input1') .transmogrify { it * 2 } // duplicate the request string .setFileHeaderFrom('destination') // set name of result file to be written (a custom DSL extension) .to('file:target/output') // replace content of file in target/output directory with body of in-message. from('direct:input2').reverse() } }
SampleModelExtension.groovy
class SampleModelExtension {
static extensions = {
...
ProcessorDefinition.metaClass.setFileHeaderFrom = { String sourceHeader ->
delegate.setHeader(Exchange.FILE_NAME) { exchange ->
def destination = exchange.in.headers."$sourceHeader"
destination ? "${destination}.txt" : 'default.txt'
}
}
}
}
Before we can start the route we additionally have to define a dependency to the camel-jetty component in our pom.xml file. pom.xml <project> <dependencies> ... <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jetty</artifactId> <version>2.0.0</version> </dependency> ... </dependencies> <project> Project testingUnit testWhen we run the unit test SampleRouteTest.java we should see a file default.txt in the target/output folder, because we didn't set a 'destination' message header. The unit test sent the message body abc so there should be a repeated abcabc contained in the output file. You run the test inside Eclipse or using Maven by entering mvn test on the command line in the basic folder. Server testFor testing the HTTP endpoint we first have to start the server. To do so, right-click on SampleServer.java and select Run As->Java Application. This will start a standalone route or integration server that listens on port 8080 for requests. As HTTP client we use the Eclipse HTTP Client. To prepare the test open the Eclipse HTTP client and
To submit the request press the green arrow at the top of the window. The result should look like: The HTTP endpoint copies all HTTP headers onto Camel message headers, so we have the destination header present for creating a custom file name. In our example the destination header value is custom and the output file therefore has the name custom.txt. You should now see this file as well as the file generated during the unit test default.txt in the target/output folder. The content of the custom.txt file should be testtest because we sent a test HTTP request body. Assembly and installationWe finally want to create a distribution package from our project, install (unzip) that package somewhere and start a standalone integration server (i.e. an integration server that runs outside Eclipse). Before continuing make sure that you stopped the SampleServer that you started within Eclipse, otherwise, you won't be able to start another server. To create the package enter mvn assembly:assembly on the command line. The created package basic-1.0-SNAPSHOT-bin.zip is written to the project's target folder. Copy the package to a new location and unzip it. This will create a folder named basic-1.0-SNAPSHOT with the following content:
Start serverTo start the server run startup.bat. The console output should like like Finally, submit the HTTP request again that you've configured before with the Eclipse HTTP client. This should again create a file custom.txt in the newly created target/output folder. SummaryIn this tutorial we've seen
|
| Document generated by Confluence on Nov 05, 2009 04:31 |