< Previous | Next >

Lesson 1.2: Create required classes and interfaces for the StatelessCounterBean.java class

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 will
  1. Open your StatelessCounterBean.java class in the Java Editor.
  2. Under the @Stateless annotation, type @Interceptors.
  3. When you press CTRL+S to save, you can see a quick-fix icon quick-fix icon beside the @Interceptors line. Right click the quick-fix icon and select Quick Fix:
  4. Select import javax.interceptor.Interceptors, and press CTRL+S to save.
  5. 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.
  6. Right click the quick-fix icon and select Quick Fix. Select Create class 'Audit'. In the Create a new Java class page, accept the defaults and click Finish.
  7. 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.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;
        }
    }
  8. 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;
        }
    }
    
  9. Right click the quick-fix icon beside the line public class StatelessCounterBean, and select Create Interface 'LocalCounter':
    Local counter error
  10. 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.websphere.ejb3sample.counter;
    
    import javax.ejb.Local;
    
    @Local
    public interface LocalCounter {
        public int increment();
        public int getTheValue();
    }
  11. Return to the StatelessCounterBean.java class in the Java editor.
  12. Follow the same steps for the Remote Counter error, 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.websphere.ejb3sample.counter;
    
    import javax.ejb.Remote;
    
    @Remote
    public interface RemoteCounter {
        public int increment();
        public int getTheValue();
    }
  13. Return to the StatelessCounterBean.java class in the Java editor.
  14. Right click the quick fix icon beside @PersistenceContext, and select Quick Fix. Select Import 'PersistenceContext' ( javax.persistence), and press CTRL+S to save.
  15. Right click the quick-fix icon beside @EntityManager, and select quick-fix. Select Import 'EntityManager' ( javax.persistence ), and press CTRL+S to save.
  16. Right click the quick-fix icon beside JPACounterEntity, and select Quick Fix. Select Create class 'JPACounterEntity.java'.
You now are ready to move on to Exercise 1.3, Create an Entity class and a database for data persistence.
< Previous | Next >

Feedback