 |
Serialize Your Thread
Asynchronous Transfer of Control Threading (ATCT) is a pure J2SE and J2EE Java framework, aiding the rapid development of software for complex distributed processes.
Through implementation of Execution Context Reification technology, ATCT is the only framework that makes it possible for asynchronous long-running process flows to be expressed and implemented in a sequential fashion, using pure Java language. ATCT allows defining asynchronous logic on the API level. It supports all the techniques and tools of traditional sequential programming into the realm of asynchronous systems.
Moreover, it provides a mechanism for interrupting and saving the execution context of sequential code threads at any point. The context can be restored and re-activated on the same machine or at a different location after transmission across a network, providing a perfect framework for gaming and asynchronous messaging applications such as Web services.
ATCT is a pure Java framework, useable with any JVM version 1.3 and up. Implemented as a set of pure Java classes, it can be used on any platform for which a JVM is available. ATCT makes the development of business processes available for J2SE and J2EE environments.
ATCT is targeted at framework development teams who create solutions to simplify the programming of asynchronous applications. Application developers using such frameworks are isolated from the complex nature of multithreaded asynchronous environments. By integrating ATCT with other development solutions, organizations are able to provide a revolutionary development environment to their clients.
http://www.velare.com/product/atct.htm
Implementing business processes in Java
How important is it to know exactly what your business process does? I would say it is very important, but when the business process involves asynchronous interaction, and they do most of the times, the tools for implementing it in a way that does not obscure the business process logic are limited. Currently, most of the business processes are coded using event handlers and a state machine, spreading business logic to multiple places in the source code.
There are several challenges with such implementation, but let me mention the following 3.
1. It's hard to introspect the business logic as it's spread across multiple source files
2. The source code does not map well to it's specification making implementation more difficult
3. The source code does not map well to any of the existing types of the UML diagrams.
One of the attempts to address this issue is to use a higher level business process execution language that would allow you to describe processes linearly. Using that approach business processes could be written in sequential manner solving many challenges, unfortunately that represents other challenges such as having to train your personnel in using that new language or difficulty in integrating it with your already working code, etc.
Allow me to introduce sample business process that customer participates in when customer buys movie tickets. To make it more fun, let's make the interaction be done via e-mail.
So, there are 2 parties that participate in this business process: the user and the automated ticket master. To initiate the process the customer needs to send e-mail to the ticket master at tickets@localhost. Ticket master replies to this e-mail with a list of movies that are showing. The customers chooses the movie and supplies that number that indicates his choice in his reply to tickets master. Ticket master replies to the e-mail with a list of show times and a request to choose the show time. Customer chooses the show time and replies to ticket master. Next, the quantity of tickets and credit card number information is being collected and confirmation is being sent to the user.
So, as you can see the regular implementation for this would be coded as a number of message handlers and a state machine that would determine next steps.
I've put together a little framework that allows you to code and deploy your e-mail based business process and below is implementation of the ticket master process the description for which is given above.
Few notes on the framework: It utilizes ATCT that literally allows you to serialize you thread at any time and then restore it and resume execution. Your process could survive anything, even JVM crash, or could even be sent over to another machine for execution, implementing some bizarre load balancing, or it could be stored for days or years on the hard drive and then restored and resume it's execution. For more details see the java doc for ATCThread
AbstractEmailProcess is a class that every e-mail business process must extend. It defines few interesting methods such as sendMessageAndGetReply. This method is a magic method as it does not return until the user replies to the e-mail that was sent by this method. This is how the linearity of the source code is achieved.
So, the source code for the business process:
package user;
import velare.alexkrut.framework.AbstractEmailBusinessProcess;
import velare.atc.AISignal;
import javax.mail.Message;
import javax.mail.MessagingException;
import java.io.IOException;
public class MovieTicketBusinessProcess extends AbstractEmailBusinessProcess {
private static String[] MOVIES = {"2 Fast 2 Furious", "Matrix Reloaded", "Italian Job"};
private static String[] SHOWTIME = {"11:00", "12:00", "13:00"};
public MovieTicketBusinessProcess(Message pMessage, String pID) {
super(pMessage, pID);
}
public void run() throws AISignal {
try {
int movie = chooseMovie();
int showtime = chooseShowTime();
int quantity = requestQuantity();
String creditCardNumber = requestCCN();
sendMessage("The credit card number you submitted is " + creditCardNumber + "\n" +
"Your order has been approved.\n You are watching " + MOVIES[movie] + " at "
+ SHOWTIME[showtime] + (quantity == 1 ? ", alone! :-)" : " with " +
(quantity - 1) + " of your friends.")
);
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private int chooseMovie() throws AISignal, MessagingException, IOException {
StringBuffer sb = new StringBuffer();
sb.append("The following movies are showing\n");
for (int i = 0; i < MOVIES.length; i++) {
String s = MOVIES[i];
sb.append(i).append(':').append(' ').append(s).append("\n");
}
sb.append("Please supply the number of the movie you'd like to watch");
Message reply = sendMessageAndGetReply(sb.toString());
int choice = readIntFromMessage(reply);
return choice;
}
private int chooseShowTime() throws AISignal, MessagingException, IOException {
StringBuffer sb = new StringBuffer();
sb.append("The following showtimes are available\n");
for (int i = 0; i < SHOWTIME.length; i++) {
String s = SHOWTIME[i];
sb.append(i).append(':').append(' ').append(s).append("\n");
}
sb.append("Please supply the number of the showtime.");
Message reply = sendMessageAndGetReply(sb.toString());
int choice = readIntFromMessage(reply);
return choice;
}
private int requestQuantity() throws AISignal, MessagingException, IOException {
Message reply = sendMessageAndGetReply("How many tickets would you like to purchase ?");
int quantity = readIntFromMessage(reply);
return quantity;
}
private String requestCCN() throws AISignal, MessagingException, IOException {
Message reply = sendMessageAndGetReply("Please supply your Credit Card Number");
String creditCardNumber = readStringFromMessage(reply);
return creditCardNumber;
}
}
The main method in this class is run method. As you can see the source code for run method maps to the specification almost directly ! exposing the business logic in code, not hiding it. More over, this opens new possibilities for programming abstract processes or using OOP techniques to expose only certain parts of the process. Or having the actual processing handled elsewhere and having the class that defines business process to only have code related to that business process. Neato !
The source code for the mini framework and business process implementation is available.
The following is a transcript of the conversation that I had with an automated ticket master.
The credit card number you submitted is 23094823423-234-23423-4234
Your order has been approved.
You are watching Matrix Reloaded at 11:00 with 3 of your friends.
>23094823423-234-23423-4234
>----- Original Message -----
>From: <tickets@localhost>
>To: "Alex Krut" <human@localhost>
>Sent: Monday, June 16, 2003 6:19 PM
>Subject: wanna watch a movie
>
>
>> Please supply your Credit Card Number
>> >4
>> >----- Original Message -----
>> >From: <tickets@localhost>
>> >To: "Alex Krut" <human@localhost>
>> >Sent: Monday, June 16, 2003 6:19 PM
>> >Subject: wanna watch a movie
>> >
>> >
>> >> How many tickets would you like to purchase ?
>> >> >0
>> >> >----- Original Message -----
>> >> >From: <tickets@localhost>
>> >> >To: "Alex Krut" <human@localhost>
>> >> >Sent: Monday, June 16, 2003 6:19 PM
>> >> >Subject: wanna watch a movie
>> >> >
>> >> >
>> >> >> The following showtimes are available
>> >> >> 0: 11:00
>> >> >> 1: 12:00
>> >> >> 2: 13:00
>> >> >> Please supply the number of the showtime.
>> >> >> >1
>> >> >> >----- Original Message -----
>> >> >> >From: <tickets@localhost>
>> >> >> >To: "Alex Krut" <human@localhost>
>> >> >> >Sent: Monday, June 16, 2003 6:18 PM
>> >> >> >Subject: wanna watch a movie
>> >> >> >
>> >> >> >
>> >> >> >> The following movies are showing
>> >> >> >> 0: 2 Fast 2 Furious
>> >> >> >> 1: Matrix Reloaded
>> >> >> >> 2: Italian Job
>> >> >> >> Please supply the number of the movie you'd like to watch
>> >> >> >> >
>> >> >> >>
>> >> >> >>
>> >> >> >> REFID:F4F97DA9D186F8D2D86F8E74BADBFD97
Alex Krut
In addition to the Discorso product that utilizes (ATCT) I have written a number of short articles on using (ATCT). I will port the articles to javapedia in the course of the next few weeks.
For now the articles can be accessed at http://www.velare.com/cgi/alexkrut-blosxom.cgi
Discussion about ATCT
|