 | Level: Advanced Sami Salkosuo (sami.salkosuo@fi.ibm.com), Software Architect, IBM
07 Jun 2006 This article shows you how to develop pervasive applications using
IBM Workplace Client Technology, Micro Edition, DB2 Everyplace, WebSphere MQ Everyplace,
WebSphere Everyplace Device Manager, and OSGi.
Prerequisites
Before beginning this article, you should:
- Install and configure WebSphere Studio Device Developer 5.7.1 or later.
- Install and configur DB2 Everyplace 8.1.4 or later for database synchronization.
- Install and configure WebSphere Everyplace Device Manager.
This article does not tell you how to install or use the software. It assumes that you are familiar with DB2 Everyplace, WebSphere MQ Everyplace, WebSphere Everyplace Device Manager, Java™, OSGi concepts and terminology, Eclipse 2.x, and the WebSphere Studio development environment.
Create an OSGi component
 |
OSGi
OSGi (Open Service Gateway Initiative) specifications define "a standardized, component-oriented,
computing environment for networked services." Using the OSGi Service Platform enables life cycle management
of applications from anywhere in the network. Because of the network-centric environment, life cycle events, such
as install, update, uninstall, and control, happen on the fly, without the need to interrupt device operation.
This is especially critical when using OSGi components in embedded systems like oil pipelines or cars. For detailed information
about OSGi, see Resources.
|
|
First we'll create an OSGi component. We'll use the famous Hello World component.
- Start WebSphere Studio Device Developer and create a new Extension services project with the name HelloWorldBundle. Be sure to create a default bundle activator.
- Open the SMF perspective and open the Package Explorer. You should see a project similar to Figure 1.
Figure 1. Hello World Bundle project
The Hello World class does not use any OSGi-specific classes and can be a Plain Old Java Object (POJO). The only requirement is that
the class must be started and stopped by the OSGi runtime. Typically this is done using a runnable interface.
- Create a new class with the name HelloWorld under the default package.
Listing 1 shows the full source code for HelloWorld, a very simple component that prints "Hello World!" once a minute to system output.
Listing 1. HelloWorld
import java.util.Date;
public class HelloWorld implements Runnable {
private boolean running = false;
private Thread thread = null;
private static HelloWorld eg = null;
private HelloWorld() {
}
public static HelloWorld getInstance() {
if (eg == null) {
eg = new HelloWorld();
}
return eg;
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
thread = null;
}
public void run() {
while (running && Thread.currentThread() == thread) {
try {
System.out.println(new Date() + ": Hello World!");
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
|
-
Open the class
ExtensionServicesBundleActivator and add the following code to the start() method.
System.out.println(new Date()+": "+getClass().getName()+" started.");
HelloWorld.getInstance().start();
|
- Add the following code to the
stop() method.
System.out.println(new Date()+": "+getClass().getName()+" stopped.");
HelloWorld.getInstance().stop();
|
Test the OSGi component
 |
IBM Service Management Framework
IBM Service Management Framework (hereafter called SMF) is an IBM implementation of the
OSGi Service Platform specification. IBM SMF provides for the network delivery and management
of applications and services independent of operating system and instruction set architecture.
SMF provides the ability to install, start, stop, update, and uninstall
applications without affecting other applications executing within the framework. The framework supports fully-connected,
intermittently-connected, and disconnected usage. See Resources for more information.
|
|
You can test your OSGi components within WebSphere Studio Device Developer. Device Developer includes an SMF bundle
server and runtime you can use to test components. To test the Hello World component.
- In the WebSphere Studio Device Developer menu select Run => Run => SMF Bundle Server, and click New.
- WebSphere Studio Device Developer creates a new bundle server. Accept the defaults and click Run.
- To submit HelloWorldBundle to the SMF bundle server, right-click the HelloWorldBundle project and select SMF => Submit bundle. Check Submit Jar and select Admin@localhost:8080/smf as the target.
- Click Finish to install the bundle to the bundle server.
Now you've installed the bundle on the bundle server. The next step is to execute the SMF runtime and install HelloWorldBundle to the runtime.
- In the WebSphere Studio Device Developer menu, select Run => Run => SMF Runtime, and click New.
- Accept the defaults and click Run to start the SMF runtime. You should now have SMF bundle server and SMF runtime
running similar to Figure 2.
Figure 2. Test environment
-
Right-click HelloWorldBundle in the Package Exporer, and click Install. The SMF bundle server installs HelloWorldBundle
to the SMF runtime, and the runtime executes the bundle immediately. In Figure 3, you can see how the bundle prints out "Hello World!" every minute.
Figure 3. Hello World!
- Right-click the bundle in the SMF runtime to start, stop, update or uninstall the bundle from the SMF runtime.
Note that the actual bundle, HelloWorldBundle, does not use any OSGi-specific interfaces or classes.
The ExtensionServiceBundleActivator class implements the OSGi interface BundleActivator, and the bundle activator
is called by SMF, which frees you, the developer, from learning OSGi details.
Create a platform builder
You can use a plaform builder to deploy applications to different devices and device configurations. Windows™ XP, Zaurus and Pocket PC are
among the supported devices. You can use the platform builder to package all the required bundles to a single distributable application, or you can include
only the required bundles.
Now let's create a new platform builder for Windows devices. The platform includes only the SMF runtime. You'll install applications to the runtime using the bundle server.
-
Select File => New => Project => Extension Services => Platform Builder, and click Next.
- Enter
HelloWorldPlatformBuilder as the project name.
- In the Platform Options dialog, select PC (Windows 2000) and click Next.
- Click Next in the J9 Options dialog.
- In the Startup options dialog, select Bundle Developer support. This enables you to use Device Developer to install bundles.
- Accept the defaults and click Finish to build the platform.
- In the project output directory, you'll see the file platform.zip. This file includes the SMF runtime. Unzip platform.zip to a directory
and execute the command:
smf/StartSMF.bat.
Figure 4. SMF runtime
Since you checked Bundle Developer support, you can use Device Developer to install bundles to the runtime.
To do that, you need to create a remote SMF runtime by doing the following:
- In the menu, select Run => Run => Remote SMF Runtime => New.
- Accept the defaults and click Run.
Device Developer connects to the remote SMF runtime just as if it were local. Now you can install HelloWorldBundle and
view the output in the SMF runtime console.
Create OSGi components for DB2 Everyplace
 |
DB2 Everyplace
DB2 Everyplace features a small footprint relational database and high performance data
synchronization that enables enterprise applications and data to be securely extended
to mobile devices, such as PDAs, smart phones and other embedded
devices and appliances. You can use DB2 Everyplace as a local independent database of the
mobile device, or query information on remote servers when a connection is available. For more information,
see Resources.
|
|
Now that you have the basics of OSGi bundles and Device Developer, you can develop bundles that use DB2 Everyplace.
The sample application is simple: random events are stored in the client database and syncronized to server.
You'll use the following bundles:
- EventGenerator creates random events and writes them to a directory.
- EventReader reads events from the directory and stores them to a database using EventDatabaseService.
- EventDatabaseService stores events to a database. Note that this is implemented as service, which means you can change the database easily without affecting EventReader.
- EventDatabaseSyncStarted starts DB2 Everyplace syncronization with the server. When bundle is started, it syncronizes with the server and creates a local database, if necessary.
You can download the source code for the DB2 Everyplace bundles from the Downloads section.
The next two sections include information about the bundles.
EventDatabaseServiceBundle
This bundle is an OSGi service and uses DB2 Everyplace synchronization. When you create a bundle project that
uses services and DB2 Everyplace synchronization, OSGi Service Tracker and DB2 Everyplace ISync Client are required. You select these
in the Platform profile dialog.
The service is presented as interface IEventDatabaseService.
Listing 2. IEventDatabaseService
public interface IEventDatabaseService {
public boolean addEvent(long time, String event, String source);
public void startSync();
}
|
Method addEvent() adds the event to DB2 Everyplace and startSync() starts the
synchronization with the server. Synchronization is handled with the ISyncSample class. This class is taken from the DB2 Everyplace samples and is
described in the DB2 Everyplace product documentation.
DB2eEventDatabase implements IEventDatabaseService and stores events to a
database using the normal JDBC API. startSync() uses ISyncSample in synchronization.
The bundle activator registers the service to the SMF runtime. The following code snippet is included in the bundle activator's start() method.
Listing 3. Register service
eventDatabase= new DB2eEventDatabase();
if (registration == null) {
registration = context.registerService(IEventDatabaseService.class.getName(),
eventDatabase, null);
}
|
Unregistering the service is done in the bundle activator's stop() method.
Listing 4. Unregister service
if (registration != null) {
registration.unregister();
}
registration = null;
|
In order to re-use packages and services in other bundles, this bundle must export required packages and
services. This is done using MANIFEST.MF. You can use the Device Developer Bundle Manifest editor
to select the required packages and services for export.
EventReaderBundle
EventReaderBundle reads events from the event directory and uses EventDatabaseService to store them to a database. The following
code snippet shows how to get a service from the SMF runtime.
Listing 5. Get service
ServiceTracker serviceTracker =
new ServiceTracker(context, IEventDatabaseService.class.getName(), null);
serviceTracker.open();
Object tmp = serviceTracker.getService();
IEventDatabaseService dbService = (IEventDatabaseService) tmp;
|
Test the DB2 Everyplace components
In order to test DB2 Everyplace components, all client machines must have DB2 Everyplace files installed (for Windows,
DLLs must be in the library path). You must have the DB2 Everyplace server installed and configured, and you need to
modify the source code to use the correct user names and host addresses.
Deploy the bundles to the SMF runtime and notice how events are generated, read, and stored to a database. Synchronization
synchronizes events to the server. This is a two-way process, so if you have several clients, each client and the server
have the same data.
Manage OSGi components with WebSphere Everyplace Device Manager
 |
WebSphere Everyplace Device Manager
WebSphere Everyplace Device Manager helps service providers simplify the management
of pervasive devices. The devices covered include cell phones, PDAs, wireless laptop computers,
automobile telematics systems, and service-gateway "black boxes" that manage utilities and
security for homes and commercial buildings. Highly scalable software can run in small implementations
managing anything from a few thousand devices up to high-end, distributed, multi-server and clustered environments
that handle millions of devices. The Device Manager supports open standards management protocols,
including OSGi and Open Mobile Alliance Device Management 1.1.2 (SyncML DM).
See Resources for more information.
|
|
In order to manage an OSGi client with WebSphere Everyplace Device Manager, you need SMF runtime with an OSGi agent.
- Create a new Platform builder project with the name SMFRuntimePlatform
- In the System bundles dialog, select bundles: SyncML/DM OSGi Agent, SyncML/DM OSGi Agent Extension, and
SyncML/DM OSGi Agent Servlet.
- Click Compute to resolve all required bundles.
Figure 5. SMF runtime with SyncML/DM agent required bundles
- Click Finish to build the platform.
- Unzip platform.zip to a directory and go to the smf directory.
- Open the OSGiAgent.properties for editing and enter the following values:
- Addr=
http://wedm60.swic.fi.ibm.com/dmserver/SyncMLDMServletAuthRequired
- UserName =
user1
- ClientPW =
password
- AccountID =
account1
- DevId =
MyDeviceID
- Mod =
SampleOSGi
- PollingEnabled =
true
- PollingStart =
06:00
- PollingEnd =
18:00
- PollingInterval =
00:02
Remember to change the WebSphere Everyplace Device Manager address.
- Verify that the Device Manager is up and running, then execute
StartSMF.bat.
The client enrolls to WebSphere Everyplace Device Manager when it first connects to the server. You can view
the parameters of the OSGi agent by opening http://localhost/osgiagentservlet in your browser.
Device Manager is used to administer client devices. Clients need only the SMF runtime
with a device agent. Device Manager never starts a connection to a client, but clients poll
the server as specified in the properties file. Administrators use the Device Manager console
to create jobs for clients.
Figure 6. Device Manager console
You can use Device Manager to create jobs for OSGi clients. Jobs include:
- Software inventory: This and other jobs are created for enrolling, currently enrolled, or both enrolled or enrolling
devices.
Figure 7. Create job
-
Software distribution: When a client connects to Device Manager and there is a distribution
job for the client, the SMF runtime downloads the software bundle and installs it on the device.
-
Software control: OSGi components can be controlled using Device Manager jobs. Install, uninstall,
start, stop and update are available for controlling OSGi components.
Devices with OSGi support are just one type of device that Device Manager supports. Other devices include
Microsoft PocketPC, Palm devices and Symbian devices.
Create OSGi components for WebSphere MQ Everyplace
 |
WebSphere MQ Everyplace
WebSphere MQ Everyplace connects mobile and embedded applications with the
enterprise using secure and dependable application messaging. Robust messaging addresses the problem
of intermittent network connectivity and provides once-only messaging, synchronous, asycnhronous and
peer-to-peer support. WebSphere MQ Everyplace includes support for Java, C, JMS and J2ME.
See Resources for more information.
|
|
OSGi components can use Java Message Service (JMS) with WebSphere MQ for reliable messaging. These bundles are also included with the
WebSphere MQ Everyplace. The sample bundles are:
MQeAdminBundle includes one class, MQeAdmin, which it exports so that other bundles can use it.
MQeReceiverBundle receives messages and prints them to system output. The actual receiving of messages uses JMS so developers can work mainly with standard Java API instead of learning new WebSphere MQ Everyplace specific APIs.
MQeSenderBundle sends messages using JMS.
The sample bundles with WebSphere MQ Everyplace show how JMS can be used in a bundle to provide
reliable messaging. Sample bundles themselves can be on different physical devices and, if you know
the IP addresses of the devices, peer-to-peer messaging is possible with WebSphere MQ Everyplace.
A typical scenario is to use JMS on the device and send messages to the enterprise core systems.
Listing 6 is an example of sending messages using WebSphere MQ Everyplace and JMS. The code is
part of MQeJMSSender class in MQeSenderBundle and it depends on having MQeAdminBundle.
Listing 6. Sending a JMS message
.
.
// Create a QueueConnectionFactory ourselves
factory = new MQeQueueConnectionFactory();
connection = factory.createQueueConnection();
// Receive calls will block if connection is not started
connection.start();
// Create session
session = connection.createQueueSession(transacted, Session.AUTO_ACKNOWLEDGE);
// Create queue
queueName = targetQMName + "+" + queueName;
ioQueue = session.createQueue(queueName);
// Create sender
System.out.println("MQeJMSSender - Creating sender");
QueueSender queueSender = session.createSender(ioQueue);
// Create a message
TextMessage outMessage = session.createTextMessage(new Date() + ": " + outString);
// Send it
queueSender.send(outMessage);
// Close everything down.
connection.close();
.
.
|
Listing 7 is an example of receiving messages using WebSphere MQ Everyplace and JMS. This code
is part of the MQeJMSReceiver class in MQeReceiverBundle, and it also depends on MQeAdminBundle.
Listing 7. Receiving messages
.
.
factory = new MQeQueueConnectionFactory();
connection = factory.createQueueConnection();
// Receive calls will block if connection is not started
connection.start();
// Create session
session = connection.createQueueSession(transacted, Session.AUTO_ACKNOWLEDGE);
// Create queue
ioQueue = session.createQueue(queueName);
// Create receiver
QueueReceiver queueReceiver = session.createReceiver(ioQueue);
// register a listener
MQeJMSReceiverListener listener1 = new MQeJMSReceiverListener(1);
queueReceiver.setMessageListener(listener1);
while (!stopping) {
//wait here untill we are told to stop. While we are waiting we could be recieving
//messages via our listener. Put in a sleep so this loop does not thrash the cpu.
Thread.sleep(2000);
}
// Close everything down.
connection.close();
.
.
//This class provides the message listener.
class MQeJMSReceiverListener implements MessageListener {
MQeJMSReceiverListener(int id) {
this.id = id;
}
/** This method provides the implementation of the MessageListener
interface. */
public void onMessage(Message message) {
System.out.println("MQeJMSReceiver - in onMessage of listener " + id);
if (message instanceof TextMessage) {
// display the message contents
System.out.println("MQeJMSReceiver - Received Message");
System.out.println(message.toString());
} else {
System.out.println("MQeJMSReceiver - error: message was not a TextMessage
as expected");
System.out.println(message);
}
}
}
|
WebSphere MQ Everyplace sample bundles, as shown here, are a good starting point for developing your own messaging applications.
In addition to Java and JMS, you can develop native Windows, Windows Pocket PC and Palm messaging applications with WebSphere MQ Everyplace.b
Summary
This article showed you how to use IBM Workplace Client Technology, Micro Edition, DB2 Everyplace, WebSphere MQ Everyplace, and WebSphere Everyplace Device Manager
to extend solutions to pervasive devices. Open Services Gateway Initiative (OSGi) and IBM technologies have solved some of the major problems developers face when introducing solutions
to pervasive devices.
Device management, messaging, and database, together with life cycle management provided by OSGi
provide powerful tools that free developers to concentrate on the business
solution at hand instead of developing basic functionality such as reliable messaging.
Download | Description | Name | Size | Download method |
|---|
| Alll bundles and their source code | wctme_bundles.zip | 8.7KB | FTP | HTTP |
|---|
Resources Learn
Get products and technologies
Discuss
About the author  | 
|  | Sami Salkosuo has worked in IBM Finland since 1999. His main area of interest is Java programming. He is a Sun Certified Java Programmer, IBM Certified Solution Developer for XML and Related Technologies, and
IBM Certified Solution Developer for IBM WebSphere Portal. In addition to Java, he also has experience with Python, Fortran, LabVIEW, Visual Basic, LISP, Perl and PHP. You can reach Sami at sami.salkosuo@fi.ibm.com. |
Rate this page
|  |