RequestServer (pattern 3.c)
This is a Java thread with a Tomcat web server. It presents to a web browser a web form, jsp page, to which you can submit pieces of data and act on them. Depending on the action, this can:
- Leave a message, without waiting for reply (pattern 3.c) with the named queue in MessageServer (pattern 2.a).
- Send, and wait for reply (pattern 3.a), a message to WeatherClient.
- Send, by a flavor of callback: unique callback (pattern 3.b), a message to WeatherClient.
RequestServer is a Java thread that has various functions. Among them, it leaves a message, pattern 3c, with the named queue in another MessageServer Java thread. It does this by constructing the message, complete with the driver control fields that tell the MessageServer receiving thread the named queue, message key, sub-key, and so on.
See the Samples "Walkthrough" video for a more detailed depiction of message flow.
As far as the receiving thread is concerned, it does not matter if the sending thread uses Java. It can be another regular system thread, as long as the necessary driver control fields are present in the message.
In this example, LeaveAMessage.java shows that with Java you can build and send such
a message to the named queue in the receiving thread. Note the API used, sendMessageToCloverleaf()
, which does not require to
wait for the reply.
package com.infor.cloverleaf.javadriver.samples.requestserver;
... (REMOVED FOR BREVITY) ...
public class LeaveAMessage extends HttpServlet {
...
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// make sure we at leave have an alphanumeric key and a message
String messageContent = request.getParameter("MSG_CONTENT");
String messageKey = request.getParameter("MSG_KEY");
String messageSubKey = request.getParameter("MSG_SUBKEY");
if (!messageKey.matches("[a-zA-Z0-9]+"))
throw new ServletException("The message key must exist and must be alphanumeric only with no spaces. You provided '" + messageKey + "'.");
if (messageContent.equals(""))
throw new ServletException("No one is interested in a blank message. Please provide some sort of message content.");
// for the rest, just check if they exist that they don't have spaces or funny characters
if (!messageSubKey.matches("[a-zA-Z0-9]*"))
throw new ServletException("Sorry, only alphanumeric values are accepted for the subkey");
String driverControl = "{MSG_KEY " + messageKey + "}" +
(messageSubKey.equals("") ? "" : " {MSG_SUBKEY " + messageSubKey + "}");
// create the message
try {
ToCloverleafMessage toCloverleafMessage = new ToCloverleafMessage(request.getParameter("USER_DATA"), driverControl, MessageTypeEnum.DATA, "LEAVE_A_MESSAGE", null, messageContent);
ToCloverleafLink.sendMessagesToCloverleaf(true, toCloverleafMessage);
} catch (Exception ex) {
throw new ServletException ("failed to either create the message or send it to Cloverleaf", ex);
}
...
} finally {
out.close();
}
}
This code is an HttpServlet. It is a part of a web application that collects information using a web form from user input in a browser. The web application is deployed on a Tomcat web server. This is a Java application that runs independently, opposed to one that is passively invoked. Therefore, in the .ini file for this thread, you must indicate the method name in the STARTMETHOD entry to invoke to start the independent application.
Another useful point is that the driver control is formatted in the
style of a Tcl keyed list. This is the practice of system drivers to format driver
control this way. You should create your entries in this way, to be compatible with
any driver control data added by ToCloverleafLink
class.
See the ToCloverleafLink
class in
the Java doc for more information on driver control.
This is an example of RequestServer.ini:
[DRIVER]
DRIVERTYPE = class # A class type or application type driver
CLASS=com/infor/cloverleaf/javadriver/samples/requestservertomcatlauncher/TomcatLauncher # The class loaded for method calls that start/stop things
STARTMETHOD = doStart # this method starts tomcat
STARTARG= apache-tomcat-7.0.8 # this specifies the tomcat folder (catalina home). It is relative to the start_DIR parameter, or it can be an absolute path.
STOPMETHOD = doStop # Do java clean up
STOPARG= # No arg required
RUNMETHOD = doReply # The outbound reply message if weather inbound
RUNARG= # Run method user argument
You then define the class TomcatLauncher
with the methods doStart()
and doStop()
.
The invocation to bootstrap.start()
is the standard way to start the Tomcat server in Java. It is not related to any of
the Java Driver API that is discussed in this section.
package com.infor.cloverleaf.javadriver.samples.requestservertomcatlauncher;
…
public class TomcatLauncher extends CloverleafLinkAbstract{
private static final Logger logger = Logger.getLogger(TomcatLauncher.class.getName());
private Bootstrap bootstrap;
@Override
public void doStart(String userS) throws Exception{
try {
// if the user supplied string starts with "/" or a drive letter then they're
// specifying the absolute path, otherwise assume it's a relative path
String catalinaHome;
if (userS.startsWith("/") || userS.matches("[a-zA-Z]:.*"))
catalinaHome = userS;
else
catalinaHome = System.getProperty("user.dir") + File.separator + userS;
// check if the file exists at least
File catalinaHomeFile = new File(catalinaHome);
if (!catalinaHomeFile.exists()){
throw new Exception("catalina home '" + catalinaHome + "' is not found, cannot start tomcat without this. Check your STARTARG.");
}
// set the catalina.home property that bootstrap relies on
System.getProperties().put("catalina.home", catalinaHome);
// fire up tomcat
bootstrap = new Bootstrap();
bootstrap.start();
// log it
logger.info("started tomcat at: " + userS);
} catch (Exception ex) {
// we failed to start tomcat, throw an exception so the thread will change to error state
throw new RuntimeException(ex);
}
}
@Override
public void doStop(String userS) {
try {
// stop tomcat gracefully
bootstrap.stop();
logger.info("stopped tomcat successfully");
} catch (Exception ex) {
logger.log(Level.SEVERE, "failed to stop tomcat properly", ex);
}
}