United States Argentina Australia Austria Belgium Canada Chile Colombia Costa Rica Dominican Republic France Europe Germany Bangladesh/India Italy Kenya Mexico Netherlands Puerto Rico South Africa Sweden Switzerland Venezuela
BASIS International Ltd.
Home | Site Map | Contact Us | Partner Login  








 
Running BBj Code From Within Java; The Java BBjBridge (Continued from page 2)
By David Wallwork

WidgetTimer.java And WidgetServlet.java

WidgetTimer.main() (see Listing C) creates a WidgetBridge and makes multiple calls to processOrder(). It prints the elapsed time for each execution of processOrder(). Since we use the same Java BBjBridge() repeatedly, this execution time becomes quite small (20-40 milliseconds). WidgetTimer can be run with a command line similar to:

java WidgetTimer   222   333    444

Listing C - WidgetTimer.java (Right click and Save to download code)


import java.io.*;
import java.text.*;
import java.util.*;
import com.basis.bbj.bridge.*;

public class WidgetTimer{
    public static void main(String p_argv[])
    {
        if(p_argv.length < 3)
        {
            System.out.println("\n\nUSAGE:\n" +
               "       java WidgetTimer widgetCount batteryCount userID\n");
            System.exit(-1);
        }
        else
        {
            try
            {
                WidgetBridge widgets = new WidgetBridge();
                for(int q=0; q<10; ++q)
                {
                    long start = System.currentTimeMillis();
                    widgets.processOrder(p_argv[0],p_argv[1],p_argv[2]);
                    long end = System.currentTimeMillis();
                    System.out.println("WidgetBridge.processOrders() " +
                            " executed in " + (end - start) + " mSec");
                }
            }
            catch(BBjBridgeException e)
            {
                e.printStackTrace();
                System.exit(-1);
            }
        }
        System.exit(0);
    }
    
}

Our final code sample is WidgetServlet.java (see Listing D), an HTTPServlet that can be deployed in a Servlet-aware web server. Again, it re-uses the same Java BBjBridge and so provides fast execution times. WidgetServlet.doGet() uses parameters from the HTTPServletRequest to call widget.bbj and prints the results to the OutputWriter of the HTTPServletResponse.

Listing D - WidgetServlet.java (Right click and Save to download code)

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.basis.bbj.bridge.*;
public class WidgetServlet extends HttpServlet
{
    WidgetBridge m_widgetBridge;
        
    /** 
     * doGet() will be called by the Servlet-aware web server in response
     * to a GET request.  It will generate HTML containing the results of
     * calling the program widget.bbj with the parameters passed into the
     * GET request.
     */

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        String title = "demo of JavaBBjBridge within Servlet";
        out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<body>");
        out.println("<h1>" + title + "</h1>");

        // retrieve the input params from request
        String widgetCount = request.getParameter("widgetCount");
        String batteryCount = request.getParameter("batteryCount");
        String userID = request.getParameter("userID");
        if( (widgetCount == null) || (batteryCount == null) ||
                (userID == null) )
        {
            out.println("\n\n ERROR:  Missing argument.  Request must contain " +
                " widgetCount, batteryCount and userID");
        }
        else
        {

        
           // initialize and run the bridge
           try
           {
                synchronized(m_widgetBridge)
                {
                    m_widgetBridge.processOrder(
                                    widgetCount, batteryCount, userID);
                    out.println(m_widgetBridge.reportResults());
                }
            }
            catch(BBjBridgeException e)
            {
                out.println("ERROR: unknown exception processing order:\n\t" +
                            e.getMessage());
            }
        }
        out.println("</body>");
        out.println("</html>");
    }

    /**
     * init() is called when this Servlet is first instantiated.  It
     * creates a new m_widgetBridge that can be used thoughout the
     * lifetime of this Servlet
     */

    public void init()
    {
        m_widgetBridge = new WidgetBridge();
    }

    /**
     * destroy is called when this Servlet is terminated.
     * It closes the m_widgetBridge
     */

    public void destroy()
    {
        m_widgetBridge.finalize();
        m_widgetBridge = null;
    }
}

Try It!

These samples should provide a starting point for the reader who wants to call BBj code from within Java. It is especially important when using the bridge that developers first debug their BBj code before attempting to call it from a Java BBjBridge. But if your BBj code is well tested then implementing the Java code that can be used to call your BBj program is rather simple.

Although the initial cost of creating a Java BBjBridge is significant, it can be reused to access your BBj code with minimal overhead.

Calling BBj from within Java is really very easy. Try it!