[jboss-user] [JBoss Seam] - Re: Starting up Asynchronous Method at startup

gbc@gmx.de do-not-reply at jboss.com
Tue Aug 21 19:02:45 EDT 2007


Works like this:

In components.xml:

  | <?xml version="1.0" encoding="UTF-8"?>
  | <components xmlns="http://jboss.com/products/seam/components"
  |      ...
  |      xmlns:async="http://jboss.com/products/seam/async" 
  |      ...>
  | 
  |    <async:quartz-dispatcher/>
  | ...
  | 

Startup class:

  | @Startup
  | @Stateful
  | @Scope(ScopeType.APPLICATION)
  | @Name("startupManager")
  | public class StartupManager implements StartupManagerLocal{ // create Interface
  | 
  | 	@Logger
  | 	private Log log;
  | 
  | 	@In(create = true)
  | 	private ProcessingActionLocal processingAction;
  | 
  | 	private QuartzTriggerHandle handle = null;
  | 
  | 	@Create
  | 	public void create() {
  | 		log.info("create(): called");
  | 		// init
  | 		int delay = 20; // in seconds
  | 		long interval = 1000 * delay;
  | 		Calendar cal = Calendar.getInstance();
  | 		cal.setTime(new Date());
  | 		cal.add(Calendar.SECOND, delay);
  | 		// create Processing
  | 		long processingId = processingAction.createProcessing("serverJob");
  | 		// schedule job and save handler
  | 		this.handle = processingAction.doServerJob(processingId, cal.getTime(), interval);
  | 		log.info("create(): id: #0", processingId);
  | 	}
  | 	
  | 	public QuartzTriggerHandle getHandle() {
  | 		return this.handle ;
  | 	}
  | 
  | 	@Remove
  | 	public void destroy() {
  | 	}
  | }
  | 

The ProcessingAction (The ProcessingBean is not shown here):

  | @Stateful
  | @Name("processingAction")
  | public class ProcessingAction implements ProcessingActionLocal { // create Interface
  | 
  | 	@Logger
  | 	private Log log;
  | 
  | 	@In
  | 	private EntityManager entityManager;
  | 
  | 	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  | 	public long createProcessing(String string) {
  | 		// create Processing
  | 		ProcessingBean processing = new ProcessingBean();
  | 		// managed context
  | 		entityManager.persist(processing);
  | 		processing.setName("serverJob");
  | 		// return id
  | 		return processing.getId();
  | 	}
  | 
  | 	@Asynchronous
  | 	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  | 	public QuartzTriggerHandle doServerJob(long processingId, @Expiration Date date, @IntervalDuration Long interval) {
  | 		log.info("doServerJob(): called");
  | 		// get bean
  | 		ProcessingBean processing = entityManager.find(ProcessingBean.class, processingId);
  | 		// if exists remove
  | 		if (processing != null) {
  | 			entityManager.remove(processing);
  | 			log.info("doServerJob(): removed: id: #0", processingId);
  | 		} else {
  | 			// else stop server job
  | 			try {
  | 				startupManager.getHandle().cancel();
  | 			} catch (Exception e) {
  | 				log.error("", e);
  | 			}
  | 			log.info("doServerJob(): stopped: id: #0", processingId);
  | 		}
  | 		// return new handle
  | 		return new QuartzTriggerHandle("serverJob");
  | 	}
  | 	
  | 	@Remove
  | 	public void destroy() {
  | 	}
  | }
  | 

Let me know if it worked out...

Greetz GHad

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4076537#4076537

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4076537



More information about the jboss-user mailing list