1 25 package org.objectweb.easybeans.tests.common.ejbs.base.timer; 26 27 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType.TIMEOUT; 28 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTERPRISE_BEAN; 29 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTITY_MANAGER; 30 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTITY_MANAGER_FACTORY; 31 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.RESOURCE_MANAGER; 32 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.TIMER; 33 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.USER_TRANSACTION; 34 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.isEqual; 35 import static org.objectweb.easybeans.tests.common.helper.ContextHelper.checkResource; 36 37 import java.io.Serializable ; 38 39 import javax.annotation.Resource; 40 import javax.ejb.EJB ; 41 import javax.ejb.EJBContext ; 42 import javax.ejb.Timeout ; 43 import javax.ejb.Timer ; 44 import javax.ejb.TimerService ; 45 import javax.jms.JMSException ; 46 import javax.jms.Message ; 47 import javax.persistence.EntityManager; 48 import javax.persistence.EntityManagerFactory; 49 import javax.persistence.PersistenceContext; 50 import javax.persistence.PersistenceUnit; 51 import javax.sql.DataSource ; 52 import javax.transaction.UserTransaction ; 53 54 import org.objectweb.easybeans.log.JLog; 55 import org.objectweb.easybeans.log.JLogFactory; 56 import org.objectweb.easybeans.tests.common.ejbs.base.ItfOneMethod01; 57 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.BaseInsertOperation; 58 import org.objectweb.easybeans.tests.common.jms.MessageProperty; 59 import org.objectweb.easybeans.tests.common.resources.EMFactoryTester; 60 import org.objectweb.easybeans.tests.common.resources.EntityManagerTester; 61 62 67 @EJB (name = "ejb/bean01", beanInterface = ItfOneMethod01.class, beanName = "EJBInjectionBean") 68 @Resource(name = "jdbc/ds01", type = javax.sql.DataSource .class, mappedName = "jdbc_1") 69 @PersistenceUnit(name = "persistence/pu01") 70 @PersistenceContext(name = "persistence/pctx01") 71 public abstract class BaseTimeoutCallbackAccess extends BaseInsertOperation implements ItfCreateTimer{ 72 73 76 private JLog logger = JLogFactory.getLog(BaseTimeoutCallbackAccess.class); 77 78 82 public abstract String getName(); 83 84 89 public abstract boolean testUserTransaction(final UserTransaction utx); 90 91 94 @Resource 95 private TimerService ts; 96 97 100 @Resource 101 private EJBContext ctx; 102 103 106 @Resource 107 private UserTransaction utx; 108 109 113 public void onMessage(final Message message) { 114 String op = null; 115 116 try { 117 op = message.getStringProperty(MessageProperty.OPERATION.toString()); 118 startTimer(DURATION, op); 119 } catch (JMSException e) { 120 logger.debug("Error getting operation type: {0}", e); 121 } 122 } 123 124 128 @Timeout 129 public void timeout(final Timer timer) { 130 String op = ""; 131 Serializable info = timer.getInfo(); 132 133 if (info != null){ 134 op = info.toString(); 135 } 136 137 if (isEqual(RESOURCE_MANAGER, op)) { 138 DataSource ds = (DataSource ) ctx.lookup("ds/ds01"); 139 checkResource(ds); 140 log(getName(), TIMEOUT, getName(), RESOURCE_MANAGER); 141 142 } else if (isEqual(ENTERPRISE_BEAN, op)) { 143 ItfOneMethod01 bean = (ItfOneMethod01) ctx.lookup("ejb/bean01"); 144 bean.getBool(); 145 log(getName(), TIMEOUT, getName(), ENTERPRISE_BEAN); 146 147 } else if (isEqual(ENTITY_MANAGER_FACTORY, op)) { 148 EntityManagerFactory emf = (EntityManagerFactory) ctx.lookup("persistance/pu01"); 149 try { 150 EMFactoryTester.checkInstance(emf, "tmpTable" + this.hashCode()); 151 log(getName(), TIMEOUT, getName(), ENTITY_MANAGER_FACTORY); 152 } catch (Exception e) { 153 logger.debug("Error in EntityManagerFactory use: {0}", e); 154 e.printStackTrace(); 155 } 156 157 } else if (isEqual(ENTITY_MANAGER, op)) { 158 EntityManager em = (EntityManager) ctx.lookup("persistance/pctx01"); 159 try { 160 EntityManagerTester.checkInstance(em, "tmpTable" + this.hashCode()); 161 log(getName(), TIMEOUT, getName(), ENTITY_MANAGER); 162 } catch (Exception e) { 163 logger.debug("Error in EntityManagerFactory use: {0}", e); 164 e.printStackTrace(); 165 } 166 167 } else if (isEqual(TIMER, op)) { 168 ts.createTimer(DURATION, "").cancel(); 169 log(getName(), TIMEOUT, getName(), TIMER); 170 171 } else if (isEqual(USER_TRANSACTION, op)){ 172 if (testUserTransaction(utx)){ 173 log(getName(), TIMEOUT, getName(), USER_TRANSACTION); 174 } 175 }else{ 176 logger.debug("Invalid operation: {0}", op); 177 } 178 } 179 180 185 public void startTimer(final int duration, final Serializable op) { 186 ts.createTimer(duration, op.toString()); 187 } 188 } 189 | Popular Tags |