4.5.4 Using Threads with Jolt

A Jolt client-side program or applet is fully thread-safe. Jolt support of multithreaded applications includes the following client characteristics:

  • Multiple sessions per client
  • Multithreaded within a session
  • Client application manages threads, not asynchronous calls
  • Performs synchronous calls

The following listing illustrates the use of two threads in a Jolt application.

Listing Using Multiple Threads with Jolt (ThreadBank.java)

/* Copyright 1996 Oracle Systems, Inc. All Rights Reserved */
import bea.jolt.*;
public class ThreadBank
{
   public static void main (String [] args)
   {
      JoltSession session;
      try  
      {
        JoltSessionAttributes dattr;
        String userName = null;
        String userPasswd = null;
        String appPasswd = null;
        String userRole = null;
        // fill in attributes required
        dattr = new JoltSessionAttributes();
        dattr.setString(dattr.APPADDRESS,”//bluefish:8501”);
        // instantiate domain  
       // check authentication level
       switch (dattr.checkAuthenticationLevel())
       {
      case JoltSessionAttributes.NOAUTH:
           System.out.println(“NOAUTH\n”);
           break;
      case JoltSessionAttributes.APPASSWORD:
           appPasswd = “myAppPasswd”;
           break;
     case JoltSessionAttributes.USRPASSWORD:
          userName = “myName”;
          userPasswd = “mySecret”;
          appPasswd = “myAppPasswd”;
          break;
}
    dattr.setInt(dattr.IDLETIMEOUT, 60);
    session = new JoltSession (dattr, userName, userRole,
                               userPasswd, appPasswd);
    T1 t1 = new T1 (session);
    T2 t2 = new T2 (session);

    t1.start();
    t2.start();
    Thread.currentThread().yield();
    try
    {
    while (t1.isAlive() && t2.isAlive())
    {
        Thread.currentThread().sleep(1000);
    }
}
catch (InterruptedException e)
{
   System.err.println(e);
   if (t2.isAlive())  
   {
   System.out.println(“job 2 is still alive”);  
   try
   {
        Thread.currentThread().sleep(1000);
   }
   catch (InterruptedException e1)
   {
       System.err.println(e1);
   }
}
else if (t1.isAlive())
{    System.out.println(“job1 is still alive”);
    try
    {
        Thread.currentThread().sleep(1000);  
    }
    catch (InterruptedException e1)
    {
        System.err.println(e1);
     }  
  }
}
        session.endSession();
  }
  catch (SessionException e)
  {
      System.err.println(e);
  }  
  finally  
  {
    System.out.println(“normal ThreadBank term”);
    }
  }
}

class T1 extends Thread
{
JoltSession j_session;
JoltRemoteService j_withdrawal;

public T1 (JoltSession session)
{
    j_session=session;
    j_withdrawal= new JoltRemoteService(“WITHDRAWAL”,j_session);
}
public void run()
{
    j_withdrawal.addInt(“ACCOUNT_ID”,10001);
    j_withdrawal.addString(“SAMOUNT”,”100.00”);
try
{
    System.out.println(“Initiating Withdrawal from account 10001”);
    j_withdrawal.call(null);
    String W = j_withdrawal.getStringDef(“SBALANCE”,”-1.0”);
    System.out.println(“-->Withdrawal Balance: “ + W);
  }
  catch (ApplicationException e)  
  {
    e.printStackTrace();
    System.err.println(e);
    }
  }
}
class T2 extends Thread
{
    JoltSession j_session;
    JoltRemoteService j_deposit;
    public T2 (JoltSession session)
{
    j_session=session;
    j_deposit= new JoltRemoteService(“DEPOSIT”,j_session);
}
public void run()
{
    j_deposit.addInt(“ACCOUNT_ID”,10000);
    j_deposit.addString(“SAMOUNT”,”100.00”);
try
{
    System.out.println(“Initiating Deposit from account 10000”);
    j_deposit.call(null);
    String D = j_deposit.getStringDef(“SBALANCE”,”-1.0”);
    System.out.println(“-->Deposit Balance: “ + D);
  }
  catch (ApplicationException e)
  {  
    e.printStackTrace();
    System.err.println(e);
    }
  }
}