Working with services
charset=UTF-8
Learning objectives
At completion of this learning module, you will be familiar with:
- The different types of steps that make up a service.
How to create a service in NexJ Studio.
How to bind a channel to a service.
How to add steps to a service, including script steps.
How to test a service.
Key concepts
Services are used to tie together integration structures. In NexJ Studio, services are part of the Integration layer. Each service defines a series of steps, similar in concept to a workflow in the Business Model layer. A service passes a token from one step to the next, performing actions on the token at each step.
The steps available to services include:
- Email - Sends an e-mail message.
- Format - Serializes a message fom an internal format to its external raw format.
- Invoke Service -Invokes an integration service.
- Log - Creates log entries.
- Parse - Deserializes a message from its external raw format to an internal format.
- Persist - Updates business model objects from an object message.
- Script - Executes custom logic from a Scheme script. The returned value is a message that is passed to the next step.
- Send - Asynchronously sends a message through a channel.
- Send/Receive - Synchronously sends a message through a channel and waits for a response. The response is a message that is passed to the next step.
- Set Channel - Sets which channel is used for output.
- Sync - Synchronizes an object message with business model objects.
- Transform - Transforms a message into another type of message, which is then passed to the next step.
Creating a service
In this learning activity, you will create a new service.
Learning activity
To create a new service called training:EchoService
:
- In the Integration layer, select the Services tab.
Right-click the list area inside the tab and select New Service. The New Service dialog opens.
Name the service
training:EchoService.
Click Finish. The new service opens in an editor dialog.
Binding an HTTP channel to the service
Let's create a new HTTP channel called training:EchoChannel
and bind it to training:EchoService
. With binding, when the channel receives a message, it passes it on to the service. When the service has finished processing the message, it sends a response message back through the channel.
Learning activity
To bind a channel to the service:
- In the Integration layer, select the Channels tab.
- Right-click the list area inside the tab and select New Channel. The New Channel dialog opens.
- Specify the following values for the channel:
- Name:
training:EchoChannel
- Channel Type:
HTTP
- Name:
Click Finish. The new channel opens in an editor.
Enter or drag and drop the following XML code on the Source tab.
XML<HTTP contentType="text/html; charset=UTF-8" get="true"> <ServiceBindings> <ServiceBinding output="training:EchoChannel" service="training:EchoService"/> </ServiceBindings> </HTTP>
Click the Overview tab and set the Content Type to
text/html; charset=UTF-8
if this is not already set.In the HTTP Methods area, ensure that GET and POST are selected.
In the Service Bindings tab, click the Add button to add a new service binding.
In the General section, specify the following values for the service binding:
Service:
training:EchoService
Output:
training
:EchoChannel
Click the Save button in the toolbar to save the channel.
This channel can be accessed through your web browser at
http://localhost:7080/training/channel/training:EchoChannel
If you go to that URL now, you will see a blank response. You need a response message.
Adding a script to the service
Let's add a script step to the EchoService
that creates a raw HTTP message.
Learning activity
To add a script step to the service:
In the Integration layer, select the Services tab and open
training:EchoService
.To add a script step to the service:
- In the Palette, select Script.
- Click a location on the line between the start and end of the service.
- Select the script icon to open its Property Editor view.
- Name:
createResponse
- Caption:
Create the response
. The caption appears in the service diagram. Description:
Create a raw HTTP response message and set its 'body' part to an HTML string with information embedded from the raw HTTP request.
- Name:
In the Script tab, enter the following code:
CODE(message (: body (format "<html><body><h1>Echo</h1> <h2>method:</h2> {0} <h2>parameters:</h2> {1} <h2>path:</h2> {2} <h2>body:</h2> {3} <h2>class:</h2> {4} </body></html>" (this 'method) (this 'parameters) (this 'path) (this 'body) (this ':class) ) ) )
Save the changes made to the service.
Testing the service
Test the service by interacting with it using a web browser and viewing the web page the service returns when accessed using the GET and POST methods.
Learning activity
Test the GET method
- In your browser, navigate to
http://localhost:7080/training/channel/EchoChannel/my/path?param1=this¶m2=that
Test the POST method
Test the POST method by creating an HTML form. The information entered into the form is processed and returned by the service.
To create the HTML form:
Create a new file called
EchoTest.html
and enter the following code:XML<HTML> <BODY> <FORM ACTION="http://localhost:7080/training/channel/EchoChannel" METHOD="POST"> <INPUT TYPE="text" name="test1"> <INPUT TYPE="submit" name="submit"> </FORM> </BODY> </HTML>
- Save your work.
- Open the file, enter a value and see the result.