Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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.

7 Comments
Labels in this area