In this tutorial we create a synchronous Web service to post Sales Orders,
but the actual posting happens in an asynchronous way.
We will also introduce a new Plugin type, the Entity Init, which
modifies the Entity before saving into database.
And (just to show different possibilities) in this case our request will
be an XML message. We also use the opportunity to create Sales Orders with
multiple line items.
We will do the following:
SYGR is designed to process huge amount of business data in the shortest possible time. In order to achieve this, a normal productive installation of SYGR is not a standalone server, but a cluster of servers, which are responsible for different tasks and share the work among each other. We differentiate four server types:
In the real life every SYGR server (and every database server) is running on a different host
computer to use the most possible computational power.
But technically it is possible to install all on one server.
In case of our demo system, we have all the four server types running on the same host and
on the same Apache Tomcat instance:
Let's start!
In this tutorial we use three entity models:
None of the models have a predefined data structure. We create all the necessary data in the
Plugins.
As a consequence, we do not create any Transformation Model or Rule, we simply do not need them now.
In case you don't know how to create the Entity Models, please review the first tutorial (and also do not forget to link the Entity Models to a Store Type).
In order to read an XML message successfully, we need to create a Java Class
which corresponds to the XML structure.
The same is true if we want to read a JSON message, but as we have seen, in case of
an Excel .csv we do not need it as that is processed as a plain text.
First we create a simple class for the items:
public class T004SoItem { |
then another for the header, including a list of items:
public class T004SoHeader { |
An example XML message which can be translated to this class:
<T004SoHeader> |
For every Sales Order we post, we need a unique number.
As we will need this new number in different places, we create a simple helper method.
This method
public class HelpUtil { |
Now we are well prepared, have everything, so let us start creating our first Plugin,
the Message Converter.
We do not repeat what we know already from previous tutorials, only show the meaningful code parts.
First we read the message content (XML) and convert to our Sales Order class.
T004SoHeader so = new T004SoHeader(); |
Then send the information whether need to wait or not to the Output Converter.
Boolean bwait = Boolean.valueOf(so.wait); |
Then, if no wait was requested we create and transfer a Sales Order number, otherwise a unique ID based on what the Output Converter can find the posted Sales Order.
String soNumber = ""; |
To initiate the Sales Order posting in an asynchronous task, we need to create a
Creator object.
We fill the first three match fields with the Sales Order number, the unique ID
and the Customer, so it will be fast searchable by these values.
Creator creator = new Creator(); |
We also need to give the name of the Entity Init Plugin, which (in waiting case) will generate the Sales Order Number and also changes the Entity Type.
creator.setInitplugin("asyncsoinit"); |
Then we create the items in the Transactional Data of the Entity.
Attr flexi = new Attr(); |
Actually it can be done easier. The util.setNodeValue()
method can
create node values in any depth, if the level names are separated by dot ("lvl1.lvl2.lvl3").
Knowing this, we do not need to create every item as a separate Attr
object:
Attr flexi = new Attr(); |
In the end just add the created Creator object to the message which is sent to the asynchronous processing.
data.messageOut.addCreator(creator); |
You can download the code from here.
In the output converter we need to know whether we have to wait for the completion of the asynchronous processing or not, and also collect the Sales Order number (if no wait) or the unique id (if wait).
Boolean bwait = false; |
If need to wait, we wait:
boolean success = util.waitFor(data.result.messageid, 10, 1000, 0);
|
In the SYGR system every incoming message (request) gets a unique ID, which is available in the Message and Output Converter Plugins and here we use it to identify which processing we want to wait for.
If the processing finished successfully, we search for the Sales Order by the unique ID (as we do not know the Sales Order number yet).
ArrayList |
Here
"st"
is the name of the Store Type to which we linked the Entity Model"SO with wait"
is the name of the Entity Model. It is set by the Entity Init Plugin."uuid"
is the match key we are looking foruuid
is the match value and"ACTIVE"
means we want only active Entities.If the search was successful, we retrieve the actual Entity.
Here we will get only one Entity, but the search method could find several,
so we need a loop (or could just take the 1st entry).
for(String potid: potids) { |
You can download the code from here.
The Entity Init Plugin is very simple.
It checks whether the Entity (coming from the Message Converter) contains a valid Sales Order number or not.
If yes, simply changes the Entity Model to "SO without wait", as the number was created by the Message Converter and we did not wait for the completion of the asynchronous process.
If no number, then creates one and changes the Entity Model to "SO with wait".
if(data.pot.getMatchval0().equals("")) { |
You can download the code from here.
We are practically done, just have to define our three Plugins in the Regio Server and link the Message and Output Converter to one or several Catcher Server.
Let's test our work with a Postman message.
The message body is the same XML we have seen earlier.
The response is plain text, but of course you can send back also XML.
First we create a Sales Order with immediate response:
Check in the Reporter Server whether it was created as expected:
It is here, open it:
We can see that
Now post another sales order, but now wait for the completion of the process.
We can see that the execution time is longer than when we did not wait.
The Sales Order now:
Very similar to the previous, just
We have arrived to the end of this tutorial.
We have learned how to use a SYGR Synchronous Web Service together
with the asynchronous processing.
See you in our next tutorial, where we go into more details of the SYGR Automation System.
If you have questions, please contact us:
contact@sygr.ch
contact@sles-automation.com
+41 79 470 67 84