United States Argentina Australia Austria Belgium Canada Chile Colombia Costa Rica Dominican Republic France 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
By David Wallwork

This article provides an example of the Java BBjBridge and how it can be used to call a BBj® program from within Java code. We will build a BBj program and a Java program that calls it (widget.bbj and WidgetBridge.java). Once this basic structure is in place, we provide two additional programs that demonstrate how a single Java BBjBridge can be reused to provide sub-second response times.

Our BBj program, widget.bbj, accepts an order for widgets and widget batteries. Based on its own somewhat esoteric business rules, it calculates bonus merchandise and dealer discount and returns these values to WidgetBridge.java.

Listing A contains the code for widget.bbj. There are several items in this program that are worth notice.

  1. The program has two modes of operation: debug and usingBridge.
  2. When running in the usingBridge mode, the program opens a device "J0".
  3. When running in the usingBridge mode, the program always terminates by doing a RELEASE.
  4. When running in the usingBridge mode, the program tests fid(0) and drops to console if fid(0) is not "IO".

Listing A - widget.bbj (Right click and Save to download code)

seterr unknownError
rem -----------------------------------------
rem determine whether we are being called by bridge
rem when not running from bridge we pass the param "debug"
rem -----------------------------------------

fileName$ = "widget.dat"
usingBridge = 1
if argc>1 and argv(1)="debug" then usingBridge = 0

rem -----------------------------------------
rem by default, the terminal for a program called from a 
rem bridge is -tIO.  The calling program can set the terminal
rem in BBjBridgeRequest.setCommandLine.  If it has been set
rem to something else then break to console.  This is 
rem mechanism to help debug interaction between this program
rem and the calling Java code
rem ----------------------------------------- 

if usingBridge AND fid(0) <> "IO" then escape
bridgeWithoutConsole:

rem -----------------------------------------
rem open a channel.  if using bridge then config.bbx
rem must contain a line similar to:
rem         "ALIAS J0 com.basis.bbj.bridge.BBjBridgeOpenPlugin"
rem -----------------------------------------

dataChannel = unt
if usingBridge then
     open(dataChannel)"J0"
else
     open(dataChannel,err=makeTestFile)fileName$
endif

rem -----------------------------------------
rem read order data
rem -----------------------------------------

read record(dataChannel,key="widgetCount",err=readError)widgetCount$
read record(dataChannel,key="batteryCount",err=readError)batteryCount$
read record(dataChannel,key="userID",err=readError)userID$

rem -----------------------------------------
rem use business rules to determine bonusWidgets, 
rem bonusMiles and discount
rem -----------------------------------------

rem need to have userID of at least three chars
bonusString$ = userID$
if len(bonusString$) < 3 then bonusString$ = "abc"

bonusWidgets = 1 + mod(asc(bonusString$(1,1)),3)
bonusMiles = 1000 + 1000*mod(asc(bonusString$(2,1)),9)
discount = 5 + mod(asc(bonusString$(3,1)),5)

rem -----------------------------------------
rem write bonusWidgets, bonusMiles and discount to
rem dataChannel
rem -----------------------------------------

bonusWidgets$ = str(bonusWidgets)
bonusMiles$ = str(bonusMiles)
discount$ = str(discount)

write(dataChannel,key="bonusWidgets",err=writeError)bonusWidgets$
write(dataChannel,key="bonusMiles",err=writeError)bonusMiles$
write(dataChannel,key="discount",err=writeError)discount$

rem -----------------------------------------
rem when running in bridge, program may return a result code to
rem the calling Java program.  We return 0 for success.
rem -----------------------------------------

release 0

rem -----------------------------------------
rem error handler if there is a problem reading from dataChannel
rem if usingBridge then do release -1.  By agreement, the
rem Java code will recognize a return code of -1 as a read error
rem -----------------------------------------

readError:
if usingBridge
    release -1
else
    escape
endif

rem -----------------------------------------
rem error handler if there is a problem writing to dataChannel
rem if usingBridge then do release -2.  By agreement, the 
rem Java code will recognize a return code of -2 as a write error
rem -----------------------------------------

writeError:
if usingBridge
    release -2
else
    escape
endif

rem -----------------------------------------
rem error handler for unknown errors
rem if usingBridge then do release -3.  By agreement, the 
rem Java code will recognize a return code of -3 as a write error
rem -----------------------------------------

unknownError:
if usingBridge
    release -3
else
    escape
endif

rem -----------------------------------------
rem routine to create file when debugging and no file exists
rem -----------------------------------------

makeTestFile:
mkeyed fileName$, 30, 0, 30
localfile = unt
open (localfile)fileName$
write (localfile,key="widgetCount")"7"
write (localfile,key="batteryCount")"5"
write (localfile,key="userID")"abc"
close (localfile)retry
(end of listing)

...Article continued on next page