EJB3.0 Development Guide: Timed Service

xiaoxiao2021-03-06  48

It can get its timing service by accessing SessionContext.

SessionContext can be obtained by injection:

Private @Inect sessionContext CTX;

In the specification of EJB2.1, it is necessary to implement the EJBTIMEOUT method, of course, EJBPassivate, EJBREMOVE, etc. In EJB3.0, you only have to create them when you want to use them, otherwise you will not be implemented.

This example has 5 files, this example of bean is a stateless session bean:

Newstimer.java: Business Interface.

Newstimer.java: Business implementation class. The EJB we develop in the future is also named (adding beans on the interface name).

Client.java: Test EJB client class.

JNDI.Properties :jndi property file provides basic configuration properties for accessing JDNI.

Build.xml: Ant profile, to compile, release, test, and clear EJB.

Here is a description of the content of each file.

Newstimer.java

Package com.kuaff.ejb3.schedule; import javax.ejb.remote; @remote public interface newstimer {public void fivenews ();

This interface defines the Fivenews method. If this method is called, a news will be output to the console after 5 minutes.

You don't have to configure its JNDI name, or you don't have to write its profile. In the EJB3.0 implemented in JBoss, you don't have to write any EJB deployment files and JBoss deployment files. JBoss defaults to use the full name of the interface as its JNDI name. In the above example, its full name can be obtained by newstimerclass.Forname ().

Newstimerbean.java

package com.kuaff.ejb3.schedule; import java.util.Date; import javax.ejb.Inject; import javax.ejb.SessionContext; import javax.ejb.Stateless; import javax.ejb.Timer; @Stateless public class NewsTimerBean implements Newstimer {private @inject sessioncontext ctx; public void fivenews () {ctx.gettimerService (). CreateTimer (New Date (). Gettime () 300000), "Ziwu has a TV station 5 minutes News: 5 minutes now, It is time to go to the instant news program. ");} Public void ejbtimeout (Timer Timer) {system.out.printf (" Time to:% s% N ", Timer.GetInfo ()); Timer.Cancel }}

Client.java

package com.kuaff.ejb3.schedule; import javax.naming.InitialContext; import javax.naming.NamingException; public class Client {public static void main (String [] args) throws NamingException {InitialContext ctx = new InitialContext (); NewsTimer timer = (Newstimer) ctx.lookup (newstimer.class.getname ()); Timer.FiveNews ();}} This class is used to test the counter EJB we publish. First pass

CTX = new initialContext (); get the context, then find NewStimer with lookup, then start the timing. .

Run {$ jboss_home} / bin's run.bat: run -c all, start JBoss.

Execute the EJBJAR TARGET in the ANT view of Eclipse. Or in the command line, enter this project directory, perform Ant Ejbjar, publish this EJB.

Execute Run Target in the ANT view of Eclipse. Or on the command line, enter this project directory, perform Ant Run, test this EJB.

转载请注明原文地址:https://www.9cbs.com/read-81466.html

New Post(0)