Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

 

IBM Lightweight Services, Part 1: Server-side scripting

Christopher Vincent, Software Engineer, Internet Technology Team, IBM Server Group
Christopher Vincent is a software engineer on the Internet Technology Team, IBM Server Group. His research interests include instant messaging, publish/subscribe infrastructure, rapid application development, and dynamic programming languages. He received M.Eng. and B.S. degrees from MIT, where he worked at the MIT Artificial Intelligence Laboratory. You can contact him at crv@us.ibm.com.

Summary:  IBM Lightweight Services (LWS) lets script developers rapidly create, deploy, and debug process-oriented services on the WebSphere platform. These script-based services integrate with IBM middleware, and extend J2EE applications with access to non-transactional resources, such as instant messaging. This article gives an introduction to LWS, which is now available for evaluation on IBM's alphaWorks site.

Part 2 of this series, "Extending J2EE with script-based agents," shows you how to extend a conventional J2EE application to include lightweight services, and how to create and manage an agent instance for each user of the system.

Date:  01 Nov 2002
Level:  Introductory

Comments:  

What are lightweight services?

IBM Lightweight Services (LWS) are long-running, server-side programs developed in lightweight programming languages such as JavaScript. We consider JavaScript a lightweight language because its relative simplicity makes it easy to learn and use. It tends to be associated with DHTML, where it is used as an event-driven scripting language in Web pages. LWS uses JavaScript as a high-level, dynamic language to develop server-side processes with access to Web services, Sametime instant messaging, and high-performance publish/subscribe.


Agents

LWS is especially suited to developing personal services, or server side applications that operate on behalf of individual users. In recognition of this programming style, the term agent is used for a program installed on the server and agent instance for a running copy of that program. Agent instances are individually created and initialized, and can be configured to service different users with varying security attributes.

An agent consists of a script paired with information about the execution environment it requires, and metadata such as author and version information. When an agent is installed, these resources are copied to the server and stored in the LWS database. Clients can then create agent instances, each with its own configuration and program state.

Consider the following simple JavaScript program:


		 
var phoneNumbers = new Object();

function setPhoneNumber(name, number) {
	phoneNumbers[name] = number;
}

function getPhoneNumber(name) {
	return phoneNumbers[name];
}

When the JavaScript code is initialized, the top-level property phoneNumbers is set with a generic object, which is used as a hash table. Notice that once the program is initialized, no code will execute until one of the two functions is invoked. Agent scripts do not have a "main loop" or a long-running sequence of top level commands. They consist of simple initialization code and JavaScript functions that act as event-handlers.


Transactions

While this style of script is already familiar to Web page developers, LWS introduces some important new features. LWS provides a programming environment that's transactional and persistent -- important features for developing robust, fault-tolerant scripts to coordinate processes on the server. Later, you'll see how the JavaScript functions above can be called by a Web services client; when the functions are invoked they run within a WebSphere transaction. For example, if a JavaScript error were to occur in the setPhoneNumber function, any modifications to the phoneNumbers property would be rolled back, or undone.

The transactional variant of JavaScript is possible because the state of each agent instance is stored in a database between events. In addition to protecting against script errors, this means that a script's program state remains valid across server failures. In the example above, the value of the phoneNumbers property will be preserved even after WebSphere is restarted.

Transactional JavaScript is useful for recovering from errors, but what about agents that communicate with non-transactional applications? An example would be Sametime instant messaging, where operations such as sending a message cannot be rolled back. To address this issue, LWS provides isolation from non-transactional resources. Most of the APIs available to agents are asynchronous, such that a result or error condition is processed by an event-handler in a new transaction. Because of this programming style, most operations can be delayed until the end of the current transaction. For example, if two Sametime messages are sent in the course of processing a JavaScript event, nothing is sent to the instant messaging server until the top level event-handler returns successfully. If a JavaScript error occurs, the messages are simply discarded.


Extensions

Standard JavaScript (ECMAScript -- see Resources) defines a basic programming language, but doesn't contain a function for communicating with other applications. In a Web page, JavaScript is extended with objects that represent HTML elements and browser features. LWS provides JavaScript extensions for communicating by Web services, Sametime instant messaging, and high-performance publish/subscribe. For example, if a LWS developer specifies a dependency on the WebServices Extension, an object named WebServices is added as a top level property. This object exposes a JavaScript API for calling remote Web services and processing the responses.

The WebServices Extension is especially interesting because it lets agents act as both Web service consumers and providers. To consume services, or act as a Web services client, an agent script constructs request objects (with or without the aid of a Web Services Description Language (WSDL) file) and calls the remote server asynchronously.

To provide Web services, the LWS tooling lets developers export a set of JavaScript functions as Web service operations. Unlike methods in Java, a JavaScript function's parameters do not specify type information. However, you can append this information in LWS, allowing JavaScript functions to accept and return specific SOAP types. Figure 1 below shows how to expose a function as a Web service in the LWS perspective for WebSphere Studio Application Developer.


Figure 1. Exposing a function as a Web service
XML error: The image is not displayed because the width is greater than the maximum of 580 pixels. Please decrease the image width.


Runtimes

This article focuses on developing services in JavaScript, but LWS also supports multiple runtimes. A runtime is responsible for executing code and storing the program state for agent instances, and is selected by the agent developer when creating a new LWS project. LWS includes a JavaScript-specific runtime based on the Mozilla Rhino engine (see Resources), and a runtime based on IBM's Bean Scripting Framework (BSF -- see Resources). The Rhino Runtime supports advanced features such as JavaScript constructors and DHTML-style event handlers, while the BSF Runtime provides a choice of several scripting languages. Note that both runtimes support JavaScript, but scripts developed for the two runtimes have a number of differences. The LWS distribution also includes BSF examples developed in interpreted Python.


Applications

LWS is especially well suited to interacting with users, whether you're developing workflow escalation agents, a CRM system, or other user-centric applications. In these scenarios, a developer would typically install an agent to handle a specific process or task. An enterprise application would then create a new instance of that agent for each user or instance of a process, supplying configuration data detailing its specific task.

You can use the phone book script above to quickly prototype a service that might otherwise be implemented using Enterprise JavaBeans technology. You can also use LWS to complement conventional WebSphere applications with functions not generally available in J2EE 1.2, including:

  • Self-scheduling (creating timers that periodically trigger script events)
  • Invoking Web services asynchronously
  • Logging into Sametime instant messaging servers.

Because of these features, LWS makes it easy to script together Web service client operations. A simple agent can dispatch multiple Web service requests simultaneously, accumulate the results, notify users, and take corrective action when operations fail.


Developing agents

LWS includes a new perspective for IBM WebSphere Studio Application Developer, which lets users rapidly modify, install, and debug agents. Because of the way agent instances are stored, installing or modifying an agent does not require any modifications to the underlying database schema. The Install button in Figure 2 below completes, within a few seconds, the following tasks:

  1. Removes any instances of the agent created earlier in this debug session.
  2. Installs (or reinstalls) the agent on the remote LWS server.
  3. Creates a new agent instance with the specified configuration information.
  4. Opens a command-line window for interacting with the new agent instance.
  5. Opens a new log window for monitoring the debug output of the new instance.

Figure 2. Installation view
XML error: The image is not displayed because the width is greater than the maximum of 580 pixels. Please decrease the image width.

For the phone book service example, you can immediately test your agent code using the Command Line view. When the user enters a JavaScript expression, it is evaluated on the server within the context of the selected agent instance and in a new transaction. Once you've confirmed that your functions work as expected, you would try to invoke the agent instance as a Web service. The release of LWS on alphaWorks (see Resources) contains tutorials on this and other development tasks.


Conclusion

I hope you'll find LWS useful for rapidly prototyping services and integrating J2EE applications with asynchronous, non-transactional middleware such as Lotus Sametime instant messaging. If you're interested in evaluating LWS you can download the server, development tools, and documentation from IBM's alphaWorks Web site (see Resources). The alphaWorks release contains several tutorials for developing agents.

See Part 2 of this series, "Extending J2EE with script-based agents," to learn how to extend a conventional J2EE enterprise application to include lightweight services and how to create and manage an agent instance for each user of the system.


Resources

About the author

Christopher Vincent is a software engineer on the Internet Technology Team, IBM Server Group. His research interests include instant messaging, publish/subscribe infrastructure, rapid application development, and dynamic programming languages. He received M.Eng. and B.S. degrees from MIT, where he worked at the MIT Artificial Intelligence Laboratory. You can contact him at crv@us.ibm.com.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Sample IT projects
ArticleID=10225
ArticleTitle=IBM Lightweight Services, Part 1: Server-side scripting
publish-date=11012002
author1-email=crv@us.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).