Вы находитесь на странице: 1из 16

ILL BE Job Scheduling from Web

Dynpro Java for CE 7.1 Using Java


Scheduler API and EJB3.0

Applies to:
SAP NetWeaver 7.1, Web Dynpro for java, JEE 5, Java Scheduler. For more information, visit the User
Interface Technology homepage.

Summary
A new job scheduler is now available as part of the SAP NetWeaver Application Server, Java EE 5 Edition.
Using EJB 3.0 job is implemented as Message-Driven Bean (MDB), which is invoked using Java Message
Service (JMS) technology.
Using scheduler API we can create tasks and set the job parameters through Web Dynpro.

Author:

Snehal Kendre

Company: L&T Infotech


Created on: 08 October 2008

Author Bio
Snehal Kendre is an Web Dynpro consultant at L&T Infotech.

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


1

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Table of Contents
Getting started ....................................................................................................................................................3
Scenario ..........................................................................................................................................................3
Creating an EJB 3.0 project................................................................................................................................3
Developing Job Bean class.............................................................................................................................5
Deploy EAR application project. .....................................................................................................................8
See The Job Definition in Java scheduler ....................................................................................................................8

Implementation of Web Dynpro project ..............................................................................................................9


Implementation of SchedulerCompView. .....................................................................................................................9

Deploy and run Web Dynpro project.............................................................................................................13


Run the Application...........................................................................................................................................13
Task information in Java Scheduler .................................................................................................................14
Successful execution of job........................................................................................................................................14

Related Content................................................................................................................................................15
Disclaimer and Liability Notice..........................................................................................................................16

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


2

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Getting started
Here we are going to create an EJB 3.0 project that includes a job bean.
The scheduler runtime service provides certain basic services to the job implementation. That is why, the
JobBean class cannot implement the onMessage() method, which is the standard business method of
message-driven beans. JobBeans have a single business method, the onJob() method. In the JobBean
class, you must provide an implementation of the onJob() method. The JobBean inherits from an
MDBJobImplementation base class which itself provides an implementation of the onMessage() method.
To set the job parameters and schedule we will create a Web Dynpro project
Scenario

We will create a job using job beans which is nothing but a message-driven bean inherited
from an MDBJobImplementation base class.
A web Dynpro project will provide basic functionality to schedule a job and to set the job parameters.

Creating an EJB 3.0 project


Open JEE perspective. Create a new EJB project as we_JobEJB

Create an EAR Application project ja_JobEAR

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


3

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Select the project facets

Add we_JobEJB into ja_JobEAR

Choose the project facets for EJB project

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


4

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Developing Job Bean class


First add tc~je~scheduler~api~jar as an external jar in build path of we_JobEjb

Create a new message driven bean JobBean

Use javax.jm.queue as JMS destination type

Select MDBJobImplementation as a supercalss for jobbean

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


5

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

And say finish.


Implementation of ejb-j2ee-engine.xml
Write down the JobBean specific settings in ejb-j2ee-engine.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-j2ee-engine
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<enterprise-beans>
<enterprise-bean>
<ejb-name>JobBean</ejb-name>
<jndi-name>JobBean</jndi-name>
<message-props>
<destination-name>JobQueue</destination-name>
<connection-factory-name>
JobQueueFactory
</connection-factory-name>
</message-props>
</enterprise-bean>
</enterprise-beans>
</ejb-j2ee-engine>
Create new xml for job information as job-definitions.xml
<job-definitions>
<job-definition name="ScheduleJob"
description="sample job for testing">
<job-definition-parameter name="User"
data-type="String"
direction="IN"/>
</job-definition>
</job-definitions>
Implementation of JobBean
/**
*
*/
package com.sap.jobbean;
import java.util.logging.Logger;
import javax.jms.MessageListener;

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


6

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0
import com.sap.scheduler.runtime.JobContext;
import com.sap.scheduler.runtime.JobParameter;
import com.sap.scheduler.runtime.mdb.MDBJobImplementation;
import javax.ejb.MessageDriven;
import javax.ejb.ActivationConfigProperty;
/**
* @author Snehal Kendre
*
*/
@MessageDriven(
//TODO The JMS destination name (mapped name) must correspond to the resource name in the jmsresources.xml
name="JobBean", mappedName="MyDestination",
activationConfig={@ActivationConfigProperty(
propertyName="messageSelector",
propertyValue="JobDefinition='Schedule_JobBean'"),@ActivationConfigProperty(propertyName="de
stinationType", propertyValue="javax.jms.Queue")})
public class JobBean extends MDBJobImplementation implements MessageListener {
/* (non-Javadoc)
* @see
com.sap.scheduler.runtime.mdb.MDBJobImplementation#onJob(com.sap.scheduler.runtime.JobContext)
*/
@Override
public void onJob(JobContext arg0) throws Exception {
// TODO Auto-generated method stub
JobParameter parameter = arg0.getJobParameter("User");
Logger logger = arg0.getLogger();
logger.info("Hi"+parameter+"your job is scheduled");
}
}

Implementation of the application-j2ee-engine.xml from ja_JobEAR


<?xml version="1.0" encoding="UTF-8"?>
<application-j2ee-engine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<provider-name>sap.com</provider-name>
<reference reference-type="hard">
<reference-target provider-name="sap.com" target-type= "service">
scheduler~runtime
</reference-target>
</reference>
<modules-additional>
<module>
<entry-name>we_JobBean.jar</entry-name>
<container-type>scheduler~container</container-type>
</module>
</modules-additional>
</application-j2ee-engine>

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


7

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Deploy EAR application project.


Open the server view

Add the EAR application project

And finish it.


See The Job Definition in Java scheduler
We can see our job definition in SAP NetWeaver Administrator.
http://<hostname>:<port>/nwa

Operation Management-> Jobs

Click on Job Definitions

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


8

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Implementation of Web Dynpro project


Open web Dynpro development perspective and create a new project dp_SchedulerApp.

Create an application.

Create New components as SchedulerComp with window SchedulerWindow and SchedulerCompView.


First add tc~je~scheduler~api~jar as an external jar in build path

Implementation of SchedulerCompView.
Create a New simple type Months

With following Enumerations

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


9

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Create context for scheduler options as follow

Assign simple type Months to the ctx_va_Month, String type to ctx_va_user and assign Integer to other
context.
Create a form to take inputs from user. For this use apply template.

Say finish and create a form.

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


10

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Create an action button to submit information to java scheduler.


Apply action button template.

Implement onActionSchedule() as follow


public void onActionSchedule(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent
wdEvent )
{
//@@begin onActionSchedule(ServerEvent)
Scheduler syncscheduler;
try {
InitialContext ctx = new InitialContext();
syncscheduler = (Scheduler)ctx.lookup("scheduler");
JobDefinition syncdef =
syncscheduler.getJobDefinitionByName("Schedule_JobBean");
//
SchedulerTaskID taskid = new SchedulerTaskID(2);

wdComponentAPI.getMessageManager().reportSuccess(syncdef.getDescription());
Job syncjob;
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.set(Calendar.YEAR,
wdContext.currentContextElement().getCtx_va_year());

calendar.set(Calendar.MONTH,wdContext.currentContextElement().getCtx_va_Month()-1);
calendar.set(Calendar.DAY_OF_MONTH,
wdContext.currentContextElement().getCtx_va_day());
calendar.set(Calendar.HOUR_OF_DAY,
wdContext.currentContextElement().getCtx_va_hour());

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


11

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

calendar.set(Calendar.MINUTE,
wdContext.currentContextElement().getCtx_va_min());
java.util.Date date = calendar.getTime();
wdComponentAPI.getMessageManager().reportSuccess("your task is scheduled
for date="+ date);
//Create SchedulerTime and pass to it the Calendar instance
SchedulerTime time = new SchedulerTime(date, TimeZone.getDefault());
//Create RecurringEntry and pass to it SchedulerTime instance
RecurringEntry re = new RecurringEntry(time);
//Create a SchedulerTask for the HelloJob
// set job paramater
JobParameterDefinition user = syncdef.getParameter("User");
JobParameter userparameter = new
JobParameter(user,wdContext.currentContextElement().getCtx_va_user());
SchedulerTask task = new
SchedulerTask(SchedulerTaskID.newID(),syncdef.getJobDefinitionId(),new JobParameter[]
{userparameter},new RecurringEntry[] {re}, new CronEntry[]
{},"Schedule_Job","scheduled by API");
syncscheduler.schedule(task);
//
task.createSchedulerTask(syncdef.getJobDefinitionId(),parm,recs,crons,filters,23,
"synctaskbywd","wd generated task",null);
//
syncscheduler.schedule(task);
} catch (Exception e) {
// TODO Auto-generated catch block
wdComponentAPI.getMessageManager().reportException("problem in
scheduler",true);
}
//@@end
}

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


12

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Deploy and run Web Dynpro project


Build project and create its archive

Deploy and run the application

Run the Application


Run the application in browser.

Get the confirmation after scheduling the job.

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


13

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Task information in Java Scheduler


We can see our job definition in SAP NetWeaver Administrator.
http://<hostname>:<port>/nwa
Check the scheduled task in Java Scheduler

Successful execution of job

Check log

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


14

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Related Content
https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/10fed553-0e01-0010-9bb8-ed55659e1236
https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/e8ab5056-0e01-0010-5581d3e51724ee21
http://help.sap.com/saphelp_nwce10/helpdata/en/44/03d66015ee10b3e10000000a11466f/frameset.htm
For more information, visit the User Interface Technology homepage.

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


15

ILL BE Job Scheduling from Web Dynpro Java for CE 7.1 Using Java Scheduler API and EJB3.0

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not
supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document,
and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or
code sample, including any liability resulting from incompatibility between the content within this document and the materials and
services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this
document.

SAP DEVELOPER NETWORK | sdn.sap.com


2008 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


16