Timing service of EJB 3.0 Development Guide

Time:2021-12-28

In EJB2 The ejbtimeout method needs to be implemented in the specification of 1. Of course, there are ejbpassivate, ejbremove and other methods. In EJB3 0, you must create them only when you want to use them, otherwise you don’t have to implement them.

There are five files in this example. The bean in this example is a stateless session bean:

  NewsTimer. Java: business interface.

  NewsTimer. Java: business implementation class. In the future, EJBs we develop will be named like this (add bean to the interface name).

  Client. Java: test the client class of EJB.

  jndi. Properties: JNDI properties file, which provides basic configuration properties for accessing jdni.

  Build. XML: ant configuration file for compiling, publishing, testing and clearing EJBs.

The following is an introduction to the contents 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 in 5 minutes.

You don’t have to configure its JNDI name or write its configuration file. EJB 3.0 implemented in JBoss 0, you don’t have to write any EJB deployment files and JBoss deployment files. JBoss uses the full name of the interface as its JNDI name by default. In the above example, its full name can be through newstimerclass Forname() gets.

  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(new Date(). Gettime() + 300000), “there is no five minute news column of TV station: now it’s time for instant news program after five minutes.”);
  }

  public void ejbTimeout(Timer timer)
  {
  System. out. Printf (“time expired:% 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 = (NewsTimer) ctx.lookup(NewsTimer.class.getName());
  timer.fiveNews();
  }
  }

This class is used to test the EJB counters we publish. First pass

  ctx = new InitialContext();

Get the context, find newstimer through lookup, and start timing..

Please run run. In the {$jboss_home} / bin directory Bat: run – C all, start JBoss.

Execute the ejbjar target in the ant view of eclipse. Or on the command line, enter the project directory, execute ant ejbjar, compile and package the EJB and publish it.

Execute run target in the ant view of eclipse. Or, on the command line, enter the project directory and execute ant run to test the EJB.

Recommended Today

Summary of ruby methods for creating arrays

Programs often need to manage variable sets. For example, the program that manages the calendar must have a list of days of the week. They must be stored in a variable every day, and their list can be stored in an array variable. Through this array variable, you can access every day. Create an empty […]