Skip to Content

There are many use cases that would require running unattended background processes in your application. The following are just a few possible alternatives how you can schedule jobs in SAP NetWeaver Cloud:

This post is about the last option. In my opinion it is the simplest one to setup and would cover the majority of the use cases.

The EJB Timer Service is the standard way in Java EE for triggering “timed notifications” for EJB beans (except for stateful session beans). The Java EE 6 Tutorial explains thoroughly how EJB Timers are used. In this post I will show the necessary steps to create an EJB Timer in SAP NetWeaver Cloud.

First, you need to download the 2.x Beta SDK instead of the 1.x SDK. The reason is that the EJB container is introduced with the 2.x Beta SDK as part of the Java EE 6 Web Profile. In 1.x SDK there is no EJB container and hence no EJB Timer Service. You can find the 2.x Beta SDK on the Tools Site, under the SDK tab, at the very bottom of the page.

Next is to configure the 2.x Beta SDK in your Eclipse IDE. This is the same procedure like with the 1.x SDK. However, if you already have the 1.x SDK configured, I highly recommend to use a separate Eclipse workspace for the 2.x Beta SDK to avoid unnecessary confusion.

You can now create new Dynamic Web project as usual. Just make sure you select version 3.0 and target it on the SAP NetWeaver Cloud runtime, so you get the EJB API properly added to the build environment of your project. Now you can use the EJB Timer wizard (File > New > Other > EJB > EJB Timer) to create a session bean with automatic timer method.

Below is a sample code of an EJB Timer that logs a message every 10 seconds between 8:00 and 23:00 from Monday to Friday:

package test;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.Timer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Stateless
public class MyTimer {
    private final static Logger logger = LoggerFactory.getLogger(MyTimer.class);
    /**
     * Default constructor.
     */
    public MyTimer() {
        // TODO Auto-generated constructor stub
    }
    @Schedule(second="*/10", minute="*", hour="8-23", dayOfWeek="Mon-Fri",
      dayOfMonth="*", month="*", year="*", info="MyTimer")
    private void scheduledTimeout(final Timer t) {
        logger.error("Automatic timer called at: {}", new java.util.Date());
    }
}

The last step is just to deploy the web project on the local server or in the cloud. When the server is started the EJB Timer is scheduled automatically and you can see the log entries appear in the ljs_trace.log file.

Note. The EJB Timer Service is not part of the Java EE 6 Web Profile specification. Therefore, it is not yet officially supported API in the 2.x Beta SDK. If there is interest in the community we may consider including the EJB Timer Service as official part of the NW Cloud API.

To report this post you need to login first.

7 Comments

You must be Logged on to comment or reply to a post.

  1. Robert Wetzold

    Great post! Do you know if the EJB Timers support clustering? I could not find anything in the documentation after a quick scan. If it did it would be a neat alternative to Quartz and would cut one dependency.

    We use Quartz all the time just because of this since in a cloud environment it is very likely you have to scale to multiple instances and then you often end up with some critical jobs which must not run in parallel at any specific point in time to not get inconsistent database entries.

    (0) 
    1. Former Member Post author

      I am not really sure. As far as I have looked at the EJB spec, clustering should be related to the persistence of the timers. But the spec does not specifies how this is implemented. I doubt the persistence of EJB Timers currently works in NW Cloud – I haven’t heard we have done any integration of OpenEJB with the Persistence Service yet.

      Although not really convenient, it should be still possible to implement database locks yourself to ensure that the job does the actual work only on one of the VMs.

      So, if it the clustering is critical for you, perhaps it is better to stay with Quartz.

      (0) 
  2. Jan Penninkhof

    Excellent post. Thank you ๐Ÿ™‚

    I’ve been using quartz in the past, which also works great. Quartz does support a clustered mode, so if scalablility is important for you, please do have a look at it. Quartz comes from the same stable as Ehcache btw, which probably enjoys a bit more fame.

    Cheers,

    Jan

    (0) 
  3. Jan Penninkhof

    Was just thinking about it… You know what would be really cool? I you could just use some simple annotations. Something like:

    @Schedule(every=”300″)

    void doStuff()

    {

    ….

    }

    If you’re thinking of integrating it in the SDK… Would love to see that….

    (0) 
  4. Former Member

    Seems like what I’m searching for (except of pre-scheduling, but this can be changed).

    But why I got this error when deploying on cloud??

    Caused by: java.lang.ClassNotFoundException: org.quartz.impl.triggers.AbstractTrigger

    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)

    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)

    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)

    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:415)

    (0) 
  5. Former Member

    Hi,

    How do you inject this EJB Timer Bean into the Servlet and make it to write to the html servlet page for every timeout ?

    Regards,

    Vishnu

    (0) 

Leave a Reply