Welcome!

Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

Scheduling Jobs in WebLogic Server

Writing Application-Specific Scheduled Jobs

The need to run scheduled jobs is increasing and becoming common across all J2EE applications. The current J2EE specification doesn't provide an easy way to schedule jobs inside an enterprise application.

We can broadly classify scheduled J2EE jobs into two categories: Server-specific scheduled jobs and application-specific scheduled jobs. This article explores how to schedule application-specific jobs inside a WebLogic Application Server.

Server-Specific vs. Application-Specific Scheduled Jobs

Server-specific scheduled jobs are associated with the lifecycle of the application server. Typically these jobs are started during the server's startup and will be active until it's shut down. As a consequence, all the resources needed for scheduled jobs have to be specified in the server's classpath.

Application-specific scheduled jobs are associated with the lifecycle of the enterprise application. They are started after the application is deployed and will be active until the application is undeployed. The resources required for application-specific scheduled jobs are packaged in the enterprise application (EAR - enterprise archive resource).

Application jobs are preferable to server jobs for the following reasons:

  • Any changes to the application jobs can be redeployed easily with the application (EAR file). For server jobs the server has to be restarted for the new changes to take effect, and this may not be desirable as it impacts the availability of other applications deployed on that server.
  • Since the scheduled jobs usually invoke business functionality, it's more logical to package them with in the EAR file.
  • By associating the resources with the EAR file, we can deploy the EAR file on another instances of WebLogic Server without having to reconfigure the resources for that server.

Schedule Jobs Inside an Enterprise Application

We will look at an example of how to implement application-specific scheduled jobs. Here we schedule two jobs that invoke business functions at certain specified intervals.

  • Order submission - invoked every day.
  • Inventory submission - invoked once a week.

In this example we're going to use WebLogic's Application Lifecycle Events in tandem with WebLogic's Timer Notifications.

These are the three steps involved in setting up scheduled jobs:

  1. Implement the Timer Notification Listener, which is responsible for adding, listening, and handling the timer notifications.
  2. Implement the Application Lifecycle Listener, which instantiates the Timer Notification Listener on desired application lifecycle events.
  3. Register the Application Lifecycle Listener in weblogic-application.xml

Implement the MyAppJobScheduler
MyAppJobScheduler will implement the javax.management.NotificationListener interface.

 


public final class MyAppJobScheduler implements NotificationListener

This class will instantiate weblogic.management.timer.Timer class and register itself as a listener for timer notifications. The Timer class is a WebLogic implementation of javax.management.TimerMBean.

 


timer = new Timer();
timer.addNotificationListener(this, null, "some handback object");

The order notification and inventory notification are added to the timer before the timer starts.

 


timer.addNotification("OrderSubmission",  "Order Submission",
this,orderSubmissionDate, DAILY_PERIOD);
timer.addNotification("InventorySubmission",
"Inventory Submission", this,inventorySubmissionDate,
 WEEKLY_PERIOD);

We will schedule the jobs so the Order job will run every day at 5 p.m. and the Inventory job will run once a week, every Friday at 10:30 p.m. Please check the code listing for details on how to construct the Date object with the specified time.

The callback method will get the notification. Based on the notification, a business process component (EJB) can be invoked to fulfill the scheduled job.

 


public void handleNotification(Notification notif, Object handback)
{
   String type = notif.getType();
    if ( type.equals("OrderSubmission") )
    {
       // invoke the component or ejb that does the order processing and submission
    }
    else if ( type.equals("InventorySubmission") )
    {
        // invoke the component or ejb that does the inventory processing and submission
    }
 }

The cleanUp method of MyAppJobScheduler stops the timer, and removes all the notifications that were added to it. We can invoke the cleanUp method as shown above from the preStop method, or it can be invoked from the finalize method of MyAppJobScheduler.

 


public synchronized void cleanUp()
{
    System.out.println(">>> MyAppJobScheduler cleanUp method called.");
    try
    {          
       timer.stop();
       timer.removeNotification(orderNotificationId);
       timer.removeNotification(inventoryNotificationId);
       System.out.println(">>> MyAppJobScheduler Scheduler stopped.");
    }
    catch (InstanceNotFoundException e)
    {
       e.printStackTrace();
    }
}

Here is how the final method of MyAppJobScheduler will look:

 


	protected void finalize() throws Throwable
  {
    	   System.out.println(">>> MyAppJobScheduler finalize called.");
    	   cleanUp();

    	   super.finalize();
  }

Implement MyAppListener
We will write a class MyAppListener that extends weblogic.application.ApplicationLifeCyleListener. Application lifecycle listener events provide handles that developers can use to control behavior during deployment, undeployment, and redeployment.

 


	public class MyAppListener extends ApplicationLifecycleListener

We want to start the scheduled jobs as soon as the application is deployed. So we will invoke the MyAppJobScheduler in the postStart method of MyAppListener. If you want to invoke the Scheduler before the startup, the invocation will be in the preStart method.

 


	public void postStart(ApplicationLifecycleEvent evt)
  {
     	   System.out.println( "MyAppListener:postStart Event");

     	   //Start the Scheduler
     	   myAppJobScheduler = new MyAppJobScheduler();
  }

We want to unschedule the jobs before the application is undeployed. So we invoke the MyAppJobScheduler's cleanUp method in the preStop method of MyAppListener.

 


public void preStop(ApplicationLifecycleEvent evt)
  {
    System.out.println( "MyAppListener:preStop Event");

    //Stop the Scheduler
    myAppJobScheduler.cleanUp();
  }

Register MyAppListener
In the weblogic-application.xml, register MyAppListener class for application lifecycle events.

 


<listener>
   <listener-class>MyAppListener</listener-class>
</listener>

Include MyAppJobScheduler, MyAppListener in the application classpath, or alternatively you can specify the jar file using <listener-uri> parameter.

 


<listener>
   <listener-class>MyAppListener</listener-class>
   <listener-uri>scheduler.jar</listener-uri>
</listener>

References

  • WebLogic Server: Programming Application Lifecycle Events: http://e-docs.bea.com/wls/docs90/programming/lifecycle.html
  • WebLogic Timer: http://e-docs.bea.com/wls/docs90/javadocs/weblogic/management/timer/Timer.html
  • J2EE Notification Listener: http://java.sun.com/j2ee/1.4/docs/api/javax/management/NotificationListener.html
  • More Stories By Vijay Chinta

    Vijay Chinta is an enterprise architect at Nokia. His primary area of expertise is J2EE development for enterprise applications in the fields of telecommunication and transportation.

    More Stories By Thomas Kunnumpurath

    Thomas Kunnumpurath is a senior software engineer at
    Nokia. He has several years experience in Java/J2EE
    application development.

    Comments (0)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    @ThingsExpo Stories
    SYS-CON Events announced today that Datera, that offers a radically new data management architecture, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Datera is transforming the traditional datacenter model through modern cloud simplicity. The technology industry is at another major inflection point. The rise of mobile, the Internet of Things, data storage and Big...
    An increasing number of companies are creating products that combine data with analytical capabilities. Running interactive queries on Big Data requires complex architectures to store and query data effectively, typically involving data streams, an choosing efficient file format/database and multiple independent systems that are tied together through custom-engineered pipelines. In his session at @BigDataExpo at @ThingsExpo, Tomer Levi, a senior software engineer at Intel’s Advanced Analytics ...
    SYS-CON Events announced today that Datera will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Datera offers a radically new approach to data management, where innovative software makes data infrastructure invisible, elastic and able to perform at the highest level. It eliminates hardware lock-in and gives IT organizations the choice to source x86 server nodes, with business model option...
    With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, Cloud Expo and @ThingsExpo are two of the most important technology events of the year. Since its launch over eight years ago, Cloud Expo and @ThingsExpo have presented a rock star faculty as well as showcased hundreds of sponsors and exhibitors! In this blog post, I provide 7 tips on how, as part of our world-class faculty, you can deliver one of the most popular sessions at our events. But before reading the...
    SYS-CON Events announced today that CA Technologies has been named "Platinum Sponsor" of SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business - from apparel to energy - is being rewritten by software. From planning to development to management to security, CA creates software that fuels transformation for companies in the applic...
    SYS-CON Events announced today that GrapeUp, the leading provider of rapid product development at the speed of business, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Grape Up is a software company, specialized in cloud native application development and professional services related to Cloud Foundry PaaS. With five expert teams that operate in various sectors of the market acr...
    Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
    In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objectively discuss how DNS is used to solve Digital Transformation challenges in large SaaS applications, CDNs, AdTech platforms, and other demanding use cases. Carl J. Levine is the Senior Technical Evangelist for NS1. A veteran of the Internet Infrastructure space, he has over a decade of experience with startups, networking protocols and Internet infrastructure, combined with the unique ability to it...
    From 2013, NTT Communications has been providing cPaaS service, SkyWay. Its customer’s expectations for leveraging WebRTC technology are not only typical real-time communication use cases such as Web conference, remote education, but also IoT use cases such as remote camera monitoring, smart-glass, and robotic. Because of this, NTT Communications has numerous IoT business use-cases that its customers are developing on top of PaaS. WebRTC will lead IoT businesses to be more innovative and address...
    In his opening keynote at 20th Cloud Expo, Michael Maximilien, Research Scientist, Architect, and Engineer at IBM, discussed the full potential of the cloud and social data requires artificial intelligence. By mixing Cloud Foundry and the rich set of Watson services, IBM's Bluemix is the best cloud operating system for enterprises today, providing rapid development and deployment of applications that can take advantage of the rich catalog of Watson services to help drive insights from the vast t...
    SYS-CON Events announced today that Elastifile will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Elastifile Cloud File System (ECFS) is software-defined data infrastructure designed for seamless and efficient management of dynamic workloads across heterogeneous environments. Elastifile provides the architecture needed to optimize your hybrid cloud environment, by facilitating efficient...
    Recently, IoT seems emerging as a solution vehicle for data analytics on real-world scenarios from setting a room temperature setting to predicting a component failure of an aircraft. Compared with developing an application or deploying a cloud service, is an IoT solution unique? If so, how? How does a typical IoT solution architecture consist? And what are the essential components and how are they relevant to each other? How does the security play out? What are the best practices in formulating...
    WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, will introduce two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a...
    When shopping for a new data processing platform for IoT solutions, many development teams want to be able to test-drive options before making a choice. Yet when evaluating an IoT solution, it’s simply not feasible to do so at scale with physical devices. Building a sensor simulator is the next best choice; however, generating a realistic simulation at very high TPS with ease of configurability is a formidable challenge. When dealing with multiple application or transport protocols, you would be...
    In his session at @ThingsExpo, Arvind Radhakrishnen discussed how IoT offers new business models in banking and financial services organizations with the capability to revolutionize products, payments, channels, business processes and asset management built on strong architectural foundation. The following topics were covered: How IoT stands to impact various business parameters including customer experience, cost and risk management within BFS organizations.
    SYS-CON Events announced today that Golden Gate University will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Since 1901, non-profit Golden Gate University (GGU) has been helping adults achieve their professional goals by providing high quality, practice-based undergraduate and graduate educational programs in law, taxation, business and related professions. Many of its courses are taug...
    SYS-CON Events announced today that DXWorldExpo has been named “Global Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Digital Transformation is the key issue driving the global enterprise IT business. Digital Transformation is most prominent among Global 2000 enterprises and government institutions.
    21st International Cloud Expo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud strategy. Me...
    SYS-CON Events announced today that Secure Channels, a cybersecurity firm, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Secure Channels, Inc. offers several products and solutions to its many clients, helping them protect critical data from being compromised and access to computer networks from the unauthorized. The company develops comprehensive data encryption security strategie...
    In his session at @ThingsExpo, Sudarshan Krishnamurthi, a Senior Manager, Business Strategy, at Cisco Systems, discussed how IT and operational technology (OT) work together, as opposed to being in separate siloes as once was traditional. Attendees learned how to fully leverage the power of IoT in their organization by bringing the two sides together and bridging the communication gap. He also looked at what good leadership must entail in order to accomplish this, and how IT managers can be the ...