Lesson 1.2 steps you through the creation of required classes
and interfaces for the StatelessCounterBean.java class.
Before you begin, you must complete Lesson 1.1.
In this lesson, you
- Add @Interceptors annotation and required dependencies.
- Create an Audit.java Java™ class.
- Add code to your StatelessCounterBean.java class
- Create a LocalCounter.java interface and a RemoteCounter.java interface
- Open your StatelessCounterBean.java class
in the Java Editor.
- Under the @Stateless annotation, type @Interceptors.
- When you press CTRL+S to save, you can see a quick-fix
icon
beside the @Interceptors line. Right-click the quick-fix icon and select Quick
Fix:
- Select import javax.interceptor.Interceptors, and press CTRL+S to save.
- Right-click the quick-fix icon and select Quick
Fix. Select Add missing attributes and replace (value={null}) with (Audit.class), press CTRL+S to save.
- Right-click the quick-fix icon and select Quick
Fix. Select Create class 'Audit'. In the Create a new Java class
page, accept the default values and click Finish.
- In the Java editor
for Audit.java, replace the default code with this
code, and press CTRL+S to save:
// This program may be used, executed, copied, modified and distributed
// without royalty for the purpose of developing, using, marketing, or distributing.
package com.ibm.example.websphere.ejb3sample.counter;
import java.io.Serializable;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class Audit implements Serializable {
private static final long serialVersionUID = 4267181799103606230L;
@AroundInvoke
public Object methodChecker (InvocationContext ic)
throws Exception
{
System.out.println("Audit:methodChecker - About to execute method: " + ic.getMethod());
Object result = ic.proceed();
return result;
}
}
- Open your StatelessCounterBean class in
the Java Editor and replace
this code, and press CTRL+S to save:
public class StatelessCounterBean {
}
with this code: public class StatelessCounterBean implements LocalCounter, RemoteCounter {
private static final String CounterDBKey = "PRIMARYKEY";
// Use container managed persistence - inject the EntityManager
@PersistenceContext (unitName="Counter")
private EntityManager em;
public int increment()
{
int result = 0;
try {
JPACounterEntity counter = em.find(JPACounterEntity.class, CounterDBKey);
if ( counter == null ) {
counter = new JPACounterEntity();
counter.setPrimaryKey(CounterDBKey);
em.persist( counter );
}
counter.setValue( counter.getValue() + 1 );
em.flush();
em.clear();
result = counter.getValue();
} catch (Throwable t) {
System.out.println("StatelessCounterBean:increment - caught unexpected exception: " + t);
t.printStackTrace();
}
return result;
}
public int getTheValue()
{
int result = 0;
try {
JPACounterEntity counter = em.find(JPACounterEntity.class, CounterDBKey);
if ( counter == null ) {
counter = new JPACounterEntity();
em.persist( counter );
em.flush();
}
em.clear();
result = counter.getValue();
} catch (Throwable t) {
System.out.println("StatelessCounterBean:increment - caught unexpected exception: " + t);
t.printStackTrace();
}
return result;
}
}
- Right-click the quick-fix icon beside the line public class StatelessCounterBean, and select Create Interface 'LocalCounter':
- Your LocalCounter.java class opens in
the Java Editor. Replace all
the code with this code, and press CTRL+S to save:
// This program may be used, executed, copied, modified and distributed
// without royalty for the purpose of developing, using, marketing, or distributing.
package com.ibm.example.websphere.ejb3sample.counter;
import javax.ejb.Local;
@Local
public interface LocalCounter {
public int increment();
public int getTheValue();
}
- Return to the StatelessCounterBean.java class in the Java editor.
- Follow the steps for creating a Local Counter to create
a Remote Counter interface, and replace all the code in RemoteCounter.java
with this code, and press CTRL+S to save:
// This program may be used, executed, copied, modified and distributed
// without royalty for the purpose of developing, using, marketing, or distributing.
package com.ibm.example.websphere.ejb3sample.counter;
import javax.ejb.Remote;
@Remote
public interface RemoteCounter {
public int increment();
public int getTheValue();
}
- Return to the StatelessCounterBean.java class in the Java editor.
- Right-click the quick fix icon beside @PersistenceContext, and select Quick Fix. Select Import 'PersistenceContext' (javax.persistence), and
press CTRL+S to save.
- Right-click the quick-fix icon beside @EntityManager, and select quick-fix. Select Import
'EntityManager' (javax.persistence), and press CTRL+S
to save.
- Right-click the quick-fix icon beside JPACounterEntity, and select Quick Fix. Select Create
class 'JPACounterEntity.java'. In the New Class wizard
page, accept the default values and click Finish
You now are ready to move on to Exercise 1.3, Create an Entity
class and a database for data persistence.