Copyright © TIBCO Software Inc. All Rights Reserved
Copyright © TIBCO Software Inc. All Rights Reserved


Chapter 3 Adapter Program Elements : MApp Application Manager

MApp Application Manager
At the center of each running adapter instance is the application manager, an instance of a subclass of MApp. All adapters need to create a subclass of MApp and define its methods.
The MApp instance handles the following tasks:
Initialization and shutdown of an adapter. Loading the configuration data (such as sessions, endpoints) and metadata into memory as part of initialization.
The SDK MApp class and its application-defined subclass implement these tasks together.
Top-Level Control Flow
Figure 4 Adapter Control Flow (C++)
 
Control Flow in C++
Figure 4 illustrates how a C++ program proceeds in single-threaded mode. In general, custom adapters run most efficiently in single-threaded mode, which is also the default mode. If a custom adapter requires multithreading, see Multithreaded Adapters.
The default steps are as follows:
1.
2.
When MApp receives the call to start(), the SDK performs internal initialization:
a.
Checks to see if MApp is already started; if it is, just returns.
b.
Reads the information in project repository and creates objects as appropriate. This includes session, endpoint, and trace objects, as well as metadata description objects.
c.
d.
3.
MApp calls the method onInitialization(), which performs application-specific initialization and must be defined for each custom adapter.
4.
The SDK executes onInitialization().
5.
When onInitialization() returns successfully, MApp behaves as follows:
If start() was called with bStartEventLoop set to true (default behavior), MApp enters a loop to dispatch events.
If start() was called with bStartEventLoop set to false, MApp returns execution control to the custom adapter, which can then call MApp::nextEvent() to dispatch any incoming event, or MRvSession::nextEvent() to dispatch events for this session.
See Event Model for information on the event model in use.
6.
Also, call stop() if the program never entered the event loop.
7.
When MApp receives the call to stop(), it calls onTermination()—defined by the custom adapter—which performs application-specific cleanup.
8.
The custom adapter executes onTermination() and returns control to the SDK.
9.
10.
11.
MApp is destroyed and control returns once more to the adapter.
 
Control Flow in Java
The control flow for Java differs slightly from that in C++. The SDK and the custom adapter interact as follows:
1.
2.
When MApp receives the call to start(), it performs internal initialization:
Checks to see if MApp is already started; if it is, just returns.
Reads the information in project repository and creates objects as appropriate. This includes session, endpoint, and trace objects, as well as metadata description objects.
3.
After MApp has performed internal initialization, it calls onInitialization(). This method performs application-specific initialization and must be defined for each custom adapter.
Components and metadata objects are already available; the custom adapter can therefore register listeners with subscribers and activate the subscribers.
4.
The SDK executes onInitialization(), then activates all components, if so specified. For some components, additional criteria must be met. For example, a subscriber must have at least one listener attached to get activated.
5.
See Event Model for information on the event model in use.
6.
7.
When MApp receives the call to stop(), it first checks whether MApp is already stopped; if so, it just returns. If not, it calls onTermination().
8.
The adapter executes onTermination() and returns control to the SDK.
9.
Creating an MApp Instance
Custom adapters set up MApp as follows:
1.
Use the TIBCO Designer software to set the adapter name, version, banner information, and other information, and save the configuration to the project repository.
2.
Programmatically create an MAppProperties instance that points to the project repository.
See MAppProperties in the online API Reference documentation.
3.
Call the constructor for MApp, passing in the MAppProperties instance, which allows access to the configuration information.
MApp is an abstract class. All custom adapters must create a subclass and implement, at a minimum, the onInitialization() and onTermination() methods. In addition, MApp offers methods to allow command-line arguments, to retrieve the current adapter name and version, and so on. See the online API documentation for MApp for details.
Examples
Example 1 onInitialization()
The following code fragment illustrates the use of onInitialization().
class ExampleApp extends MApp
{
 
ExampleListener listener;
public ExampleApp(MAppProperties appProperties )
{
super(appProperties );
}
 
protected void onInitialization() throws MException
{
// attach listener for a subscriber for discovery messages
MComponentRegistry componentRegistry =       getComponentRegistry();
MSubscriber sub = componentRegistry.getSubscriber(       "DiscoveryListener" );
sub.addListener( listener );
 
// attach to a monitored object to a Hawk microagent
MHawkRegistry hawkRegistry = getHawkRegistry();
Object monitoredStats = new Stats( this );
MHawkMicroAgent hma = hawkRegistry.getHawkMicroAgent(       "GetStatistics" );
hma.setMonitoredObject(monitoredStats );
}
}
Example 2 onTermination()
The following code fragment illustrates the use of onTermination().
public void onTermination() throws MException
{
// application specific database cleanup
dbConn.commit();
dbConn.close();
// global notification that application is stopping
MPublisher publisher = getComponentRegistry().getPublisher(    "Alert-sender" );
Minstance message =    getClassRegistry().getDataFactory().newInstance( “Alert” );
message.set( "status", "Stopping" );
publisher.send(message );
}

Copyright © TIBCO Software Inc. All Rights Reserved
Copyright © TIBCO Software Inc. All Rights Reserved