Programmatic Timer
Start course

In this course, we will learn the concepts of Java EE 7 with a focus on Enterprise Java Beans (EJB).

Learning Objectives

  • Enterprise Java Beans (EJB)

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers


  • Have at least 2 years of Java development experience 

Hello dear friends. In this video, we will try to use a timer service in a programmatic way. So let's start. In EJB, we have two ways to use the programmatic timer. The first one is the timeout annotation and the second one is schedule annotation. In this video, we will use the timeout annotation. In some cases, it may take some time before action is taken. For example, suppose you requested a transaction and you couldn't get the transaction's return right away, but you could get it after an hour. Here, we can use the EJB timer service, provide the necessary information, and then go and get the process results after one hour.

So let's create a dynamic web project.

Select WildFly application server from the target runtime. Let's give it a name. The name will be Programmatic Timer. Click 'Modify' . I want to make this project with Java Server Faces. Add JSF to the project. Mapping will be XHTML. And finish. Now, our web project is created. Let's move on. Let's add our first page. Right click on the web folder, select 'New', select a new XHTML page. The name will be the index. Done.

Now, we must add <h> tags to our page. Done. Let's continue with the <title>. Now, define the <body> tag. In the body, I want to add a <form> tag. In form, we can define a grid. In JSF videos, we learned the usage of all tags. One column will be enough for us. Let's add an output text. The value will be name. To get value from the user, I want to add an input text.

Also, we will need a button. I will use the 'Command' button. Let's define bean. Now, we need a package and a class. Now, add a new class to the package. The name will be Timer Bean. We need name annotation. I want to use this bean with a session scope. Add libraries. Now the class must implement Serializable. In class, I only need the name field. Generate Getter and Setter. Done. Go back to the index page. Now, I will hold value with the name field in bean. Done. Now let's define an action method. In the 'Command' button, I will call the action method. Done. The index page is ready.

Now we need to create an interface. We use local annotation here. Since we will use the EJB timer service in a session bean, we need an interface. It's up to you whether to use remote or local here. If your project will connect to the outside world, you will need to use remote. You can use remote and local for the same JVM, and add a method. The name will be called EJBTimerService(), and add parameters. We need a session bean class now. Let's add and use stateless annotation.

In session bean, we define a session context object with @Resource annotation. We will use this object when we obtain a timer service object. We access the CDI bean with the @Inject notation and define a CDI bean object. First, we need a session context. After that we will override or call EJBTimerService() method. In the method, first I want to show the created time.  Implements interface.

Now, from the session context, we will call the get timer service method and we will create our timer. We pass two parameters to the create timer method and one of them is duration, which tells how long the action will take when the create timer method is called. It waits for the duration given to it, and when the time expires, it finds and calls the method marked with timeout. Let's define the timeout method. We need a JSF team now. Let's create. Go back to the bean. Now in Bean, first we will use EJB annotation. Now define the EJB timer interface as private.

In the action method, we can call our EJBTimerService(). Since we know almost all the expressions in the JSF beam class, there is nothing extra to tell friends. I just want to mention the setName() method. When calling the EJBTimerService() method within the setName() method, we multiply the duration variable by 1,000. Because when calling the createTimer() method, we give a parameter of type, long, and createTimer() takes it in milliseconds. Since one second is 1,000 milliseconds, we multiply it by 1,000 when sending. We also pass the name variable to call EJBTimerService() method. The second parameter will be the data that we will process after a certain period of time.

Let's also add a process method. Now, also inject timer bean. Done. After the process, we have to cancel the timer. Done. Now our app is almost ready. Let's try. Enter a name. Look, the timer is created. After five seconds, look, our message is here. Everything works out smoothly. Now, we have finished this project. With this project, we finished the EJB topic. In the next video, we will solve the topic questions of EJB. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics