Ejb3.0 Development Guide: timed Service

Source: Internet
Author: User
You can access sessioncontext to obtain its scheduled service.

Sessioncontext can be obtained through injection:

Private @ inject sessioncontext CTX;

The ejbtimeout method must be implemented in ejb2.1 specifications. Of course, ejbpassivate, ejbremove, and other methods are also required. In ejb3.0, you must create them only when you want to use them. Otherwise, you do not need to implement them.

This example mainly contains five files. The bean in this example is a stateless Session Bean:

Newstimer. Java: business interface.

Newstimer. Java: Business implementation class. In the future, the EJB we developed will also be named like this (add bean to the interface name ).

Client. Java: client class for testing EJB.

JNDI. properties: The JNDI property file that provides basic configuration properties for accessing jdni.

Build. xml: ant configuration file for compiling, publishing, testing, and clearing ejbs.

The following describes 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 piece of news will be output to the console in 5 minutes.

You do not need to configure its JNDI name or write its configuration file. In ejb3.0 implemented by JBoss, you do not have to write any EJB deployment files or JBoss deployment files. By default, JBoss uses the full name of the interface as its JNDI name. In the preceding example, the full name can be obtained through 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 newstments

{

Private @ inject sessioncontext CTX;

Public void fivenews ()

{

CTX. gettimerservice (). createtimer (new date (). gettime () + 300000), "ziwuwuyou TV station's 5-minute news topic: Now it's five minutes, and it's time for instant news programs. ");

}

Public void ejbtimeout (timer)

{

System. Out. printf ("Time to: % N % 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 = (newstmer) CTX. Lookup (newstmer. Class. getname ());

Timer. fivenews ();

}

}

This class is used to test the counter EJB we released. First pass

CTX = new initialcontext (); obtain the context, then use lookup to find newstcontext, and start the timer ..

 

Run run. BAT: Run? In the {$ jboss_home}/bin directory? C All: Start JBoss.

Execute ejbjar target in the ant view of Eclipse. Alternatively, go to the project directory under the command line and execute ant ejbjar to package the compilation and release the EJB.

Run target in the ant view of Eclipse. Or enter the project directory under the command line and run ant run to test the EJB.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.