eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'ActiveObject':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: ActiveObject


Inheritance:

   Object
   |
   +--ActiveObject

Package:
stx:libbasic2
Category:
Kernel-Processes
Version:
rev: 1.4 date: 2019/06/25 05:58:37
user: cg
file: ActiveObject.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Claus Gittinger

Description:


an active object executes incoming messages in a serialized, synchronous manner,
enqueuing incoming messages, executing them one after the other, and returning results to the caller.
Messages are synchronous: the caller is blocked while I perform my duty.
Exceptions during execution are sent back to the caller.
This is an abstract framework class, to be subclassed for real workers.

The following is a not-yet-working experiment, using lookup objects to redirect automatcally into a synchronizing
method. This does not work yet.

Notice the use of the lookup object here: all incoming messages from a process other than my worker-process
itself are forwarded to the #doesNotUnderstand: method. There is no need to inherit from nil, and subclasses can use
any messages they like locally, without them being enqueued.

For now, define methods which have to be synchronized by defining them as:
    methodX
        self synchronized:[
            ...
            do something
            ...
        ]


Class protocol:

initialization
o  initialize
self lookupObject: RedirectingLookup new

instance creation
o  new


Instance protocol:

accessing
o  process

message handling
o  messageHandlingLoop
Transcript showCR:'A: await message...'.

o  sendAction: aBlock
Transcript showCR:'starting worker thread'.

o  sendSelector: selector arguments: arguments
Transcript showCR:'starting worker thread'.

o  synchronized: aBlock
evaluate aBlock synchronized, i.e. use a monitor for this object;
return the value from aBlock

o  synchronizedMethod
marks calling methods as being handled via the queue


Private classes:

    MessageAndResponse
    RedirectingLookup

Examples:


normally, one would subclass ActiveObject and put protocol into it; here, for the example, an anon Printer is defined. It is slow printing to the Transcript for a demo. The interesting thing is the error handling which is demonstrated in printWithError: Any exception inside the worker object is returned back and raised in the active-object's client, not in the worker (take a look at the call-chain, when running the example below).
  |workerClass worker|

  workerClass := ActiveObject 
                      subclass:#Printer
                      instanceVariableNames:''
                      classVariableNames:''
                      poolDictionaries:''
                      category:nil
                      inEnvironment:nil.

  workerClass compile:'
print:aLine
  self synchronized:[.
      aLine do:[:ch |
          Transcript show:ch.
          Delay waitForSeconds:0.2.
      ].
      Transcript cr.
  ]
'.

  workerClass compile:'
printWithError:aLine
  self synchronized:[.
      aLine do:[:ch |
          Transcript show:ch.
          ch == $l ifTrue:[ self foo ].
          Delay waitForSeconds:0.2.
      ].
      Transcript cr.
  ]
'.

  worker := workerClass new.
  'now ready for printing'.
  worker printWithError:'abcdef'.
  worker printWithError:'hello world'.


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Tue, 31 Jan 2023 20:07:29 GMT