Published on dev2dev (http://dev2dev.bea.com/)
http://dev2dev.bea.com/pub/a/2007/07/building-event-server-app.html
See this if you're having trouble printing code examples
by Robin Smith
07/16/2007
WebLogic Event Server, the next-generation event-driven SOA real-time Java container, is here. It encompasses the use of an event processing network of nodes with OSGi bundles and the Spring framework—a powerful combination.
The purpose of this article is to guide you through a step-by-step process for creating your first WebLogic Event Server application. You can very quickly create and publish (deploy) your application and see it in action. After completing the tutorial, you'll be well on your way to understanding the design principles of real-time event-driven architecture.
Before we start the development process, let's review some fundamental concepts. Your application will model a complex event processing (CEP) network (see Figure 1). This will encompass a collection of collaborating producers and consumers of events with nodes (stages) that reflect:
Figure 1. Event-driven programming model
The new Java container fully supports Spring. This leading full-stack Java application framework provides significant benefits for development projects, increasing development productivity and runtime performance while improving test coverage and application quality.
The event-driven programming paradigm supported by the WebLogic Event Server provides for a Spring-based declarative assembly and configuration (see Figure 2), which enables applications to be configured in Spring configuration files. These files incorporate custom Spring tags for the WebLogic Event Server services, seamless integration to legacy Spring beans, and dependency injection capabilities for additional services, such as those provided by the SOA fabric offered by the BEA product portfolio.
Figure 2. Example of the HelloWorld Spring Assembly File
Another major aspect to a complex event processing (CEP) network is that the processors derive the "events" from one or more incoming data streams with the use of the BEA Event Processing Language (EPL). Its use in the context of an event processor can be seen as analogous to the database management of structured data—but backwards. Rather than the information remaining static with SQL queries being executed ad-hoc across the data, in an event-driven application the queries remain static and the data is continuously changing. The processor(s) (meaning the CEP engine) "assimilates" EPL as it continuously filters, correlates, and manages causality, and then aggregates the data streams, all executed in soft real time.
EPL is our SQL-like event processing language that drives the processor(s). It extends the traditional capabilities of SQL by using WHEN rather than IF clauses, therefore handling the required temporal constraints to define windows in the streaming data in which event patterns can be identified and acted upon accordingly. Here is an example of EPL:
<n1:config>
<processor>
<name>helloworldProcessor</name>
<rules>
<rule id="helloworldRule"><![CDATA[
select * from HelloWorldEvent retain 1 event ]]></rule>
</rules>
</processor>
<adapter>
<name>helloworldAdapter</name>
<message>HelloWorld - the current time is:</message>
</adapter>
</n1:config>
Listing 1. EPL used in the HelloWorld example
The "retains" clause in the simple EPL statement in Listing 1 defines the window of events that are visible to the query. In this case, the retains value of 1 means that we simply want to see each event once. The code that processes these events is shown in Listing 2.
EPL is a powerful language that enables you, the developer, to implement a comprehensive range of queries to "extract" the events from the incoming data and invoke your business logic (POJO). What is really cool is that EPL is defined outside the programmatic aspects of the application, which can facilitate high developer productivity and flexibility. In fact, you can dynamically alter queries without recompiling, bundling and republishing the associated application to enable your solution to react to changing conditions or circumstances in real time.
Okay, enough with the programming concepts. Let's look at the container quickly, before plunging into some code.
As you might have suspected by now, BEA is not offering any traditional extensions to its leading Java EE Application Server or add-ons to its other extensive array of products. The WebLogic Event Server is a completely new, built-from-the-ground-up Java container, based on the latest BEA msA (micro-services architecture). At the core of its design is an infrastructure for providing deterministic, extremely low application latencies under predetermined peak workloads—an essential requirement for high-performance real-time applications. Unlike other solutions, where the developer needs to program to new extension APIs or complex self-memory management implementations, this new Java container achieves its goals by adopting transparently for the developer real-time concepts, such as dynamically minimizing thread context switching and synchronization, tuning I/O (such as socket connections), and incorporating real-time scheduling techniques and efficient memory management.
All of these capabilities can be augmented with the benefits offered by another product in the Event-driven SOA (EDSOA) family: WebLogic Real Time. This product, a subject for other more detailed articles, can provide a unique foundational runtime for the WebLogic Event Server, with its own low latency-focused Java virtual machine (JVM) implementation (see Figure 3).
Figure 3. A unique deterministic Java runtime and Java container
Now that I have teased you with new concepts and implementations, let me now introduce the BEA Eclipse rapid application development (RAD) environment for the WebLogic Event Server.
While it is very important that you are aware of the concepts and rich features offered by these new technologies, BEA is now providing you with a collection of intuitive wizards for the dynamic construction and publishing of WebLogic Event Server applications.
Before building our first EDA application (the traditional "HelloWorld"), you need to make sure you have installed all of the required software. I recommend that you use Eclipse 3.2 with WTP, and then use the "feature updates" capability to get the latest version of Spring. Now you are ready to install the custom BEA Eclipse Technology Preview of the BEA WebLogic Server Plug-in (see Figure 4) by once again using the "feature updates" with the Local Site option.
Figure 4. Install the WebLogic Event Server Eclipse plug-in
|
Figure 5 highlights the components that make up the HelloWorld event processing network (application).
Figure 5. The HelloWorld event processing network
The application includes the following components:
Here's the code for the HelloWorldBean:
public class HelloWorldBean implements EventSink {
public void onEvent(List newEvents) throws RejectEventException {
for (Object event: newEvents) {
HelloWorldEvent helloWorldEvent = (HelloWorldEvent) event;
System.out.println("Message Robin: " + helloWorldEvent.getMessage());
}
}
}
Listing 2. The HelloWorldBean class
As Listing 2 shows, any event handler simply needs to implement the EventSink
interface. The onEvent() method will then be called. Note that it takes a
List of an event as an argument. This is because EPL statements can generate more that one associated event object. However, in the case of our HelloWorld example, we will only receive one event in the list.
HelloWorldAdapter, which generates events, is pretty simple. It just calls a method
every 300 milliseconds which generates the event:
public class HelloWorldAdapter extends ActiveAdapter {
/* ... */
private void generateHelloMessage() {
List eventCollection = new ArrayList();
String message = this.message + dateFormat.format(new Date());
HelloWorldEvent event = new HelloWorldEvent();
event.setMessage(message);
eventCollection.add(event);
fireEvent(eventCollection, null);
}
}
Listing 3. An excerpt from the HelloWorldAdapter class
As you can see, sending a message is just a matter of subclassing ActiveAdapter,
creating an Event instance, populating it, and then calling fireEvent().
Once the software has been successfully installed, you can start by selecting File->New -> Project in Eclipse, and selecting Event Server Application Project (see Figure 6).
Figure 6. Getting started with a WebLogic Event Server project
The rest of the processing provided by the wizard is now rather intuitive but is described in more detail in the EventServerToolingPreview.Getting.Started.Guide.pdf provided with the BEA software.
Follow the few wizard steps, providing a new application name and the Target WebLogic Event Server runtime, and most importantly selecting the "include the HelloWorld source code" selection box. In a matter of a few seconds, a new project will be dynamically created with all of the artifacts that you will need to deploy (publish) a fully functional solution.
Before I leave you to the excitement of exploring this new world of complex event processing networks, let me highlight some of the resulting artifacts when using the Eclipse IDE Event Server Plug-in, and a few recommendations (see Figure 7).
Figure 7. Rapid application development with the Eclipse IDE
In the "src" section you can find your Java code for your Event Server project. (If you do not see any HelloWorld source, then you forgot to select the option for the "helloworld" templates to be added to this project in the wizard.) To start implementing your own, the helloworldBean.java is a good place to jump in and perhaps modify the message output associated with the events.
In the META-INF section (under Spring), you can locate the Application Spring Assembly file that wires up all of the various nodes (stages) encompassed in the solution. This is where the powerful code completion IDE capabilities can be used to create additions to your complex event processing network.
Finally, again under the META-INF section (under wlevs), you can find the EPL definition file where you can specify the wealth of statements and related clauses to query the data arriving to this application (via the adapter or adapters).
It can be challenging to construct applications (especially as you investigate more of the capabilities of the WebLogic Event Server) because EDA requires navigating around OSGi bundles, Spring Assembly Files, EPNs, and several other components in the mix. However, with this first plug-in, with its evolving set of new IDE features arriving soon, we are providing developers with capabilities to simplify the dynamic creation and deployment (publishing) of our new WebLogic Event Server applications.
With this new Java application server and tooling, BEA is arming architects and developers with the capabilities to ensure that event-driven SOA solutions work effectively and can be delivered within cost and infrastructure parameters. We enable IT to introduce new types of services and SLAs, which can lead to higher revenues. The Java Application Server also helps IT provide better customer satisfaction and retention, which again can boost revenues. EDSOA promotes more agility with proactive operations, and this can make your organization more competitive and reduce operational costs. This unique new runtime and infrastructure can help drive rapid development for a tremendous time-to-market advantage.
Robin Smith is the Senior Engineering Product Manager at BEA Systems for the WebLogic Time and Event Driven (TED) Product Family.
Return to Dev2Dev.