@Singleton public class BeanMessage implements BeanMessageInterface { private String message = ""; @Override public String getMessage() { return message; } @Override public void setMessage(String message) { this.message = message; System.out.println("BeanMessage: setMessage=" + this.message); } @Override public void appendMessage(String message) { this.message += message; System.out.println("BeanMessage: appendMessage=" + this.message); } @Override public String sayHello() { return "Hello from: " + this.getClass().getName() + "; " + System.identityHashCode(this); } }
@Singleton public class EjbReloaderService {
/** * Singleton EJB counter bean as a JAX-RS resource. * The bean is for one published as a standalone JAX-RS resource * and for two used to inject other EJB based JAX-RS resources. * * @author Jakub Podlesak (jakub.podlesak at oracle.com) */ @Singleton @Path("counter") public class EjbCounterResource { final AtomicInteger counter = new AtomicInteger(); @Context UriInfo ui; @GET public int getCount() { return counter.incrementAndGet(); } @Path("{ui}") @GET public String getUi() { return ui != null ? ui.getPath() : "UriInfo is null"; } }
/** * @author arungupta */ @Startup @Singleton public class MyTimerScheduleBean extends AbstractTimerBatch { }
@Startup @Singleton public class PingsListener { final List<Ping> pings = new CopyOnWriteArrayList<>(); public void listen(@Observes Ping ping) { System.out.println("ping = " + ping); pings.add(ping); } public List<Ping> getPings() { return pings; } }
/** * JAX-RS resource that keeps number of reloads. * * @author Jakub Podlesak (jakub.podlesak at oracle.com) */ @Provider @Singleton public class ContainerListener extends AbstractContainerLifecycleListener { @EJB EjbReloaderService reloader; @Override public void onStartup(final Container container) { reloader.setContainer(container); } }
/** * @author Jacek Jackowiak */ @Startup @Singleton public class SchedulesTimerBean { @Inject Event<Ping> pingEvent; @Schedules({ @Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer"), @Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer") }) public void automaticallyScheduled(Timer timer) { pingEvent.fire(new Ping(timer.getInfo().toString())); } }
/** * @author Arun Gupta */ @Startup @Singleton public class AutomaticTimerBean { @Resource SessionContext ctx; @Inject Event<Ping> pingEvent; @Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer") public void printDate() { Collection<Timer> timers = ctx.getTimerService().getAllTimers(); for (Timer t : timers) { pingEvent.fire(new Ping(t.getInfo().toString())); } } }
@Singleton public class BeanLeaf { private static final String MESSAGE_POST = "PostBeanLeaf"; private static final String MESSAGE_HELLO = "HelloBeanLeaf"; @EJB private BeanMessageInterface msg; @PostConstruct public void afterConstruct() { if (msg != null && !msg.getMessage().contains(MESSAGE_POST)) { msg.appendMessage(MESSAGE_POST); } } public String sayHello() { if (msg != null && !msg.getMessage().contains(MESSAGE_HELLO)) { msg.appendMessage(MESSAGE_HELLO); } return "Hello from: " + this.getClass().getName() + "; " + System.identityHashCode(this); } }
/** * @author Jacek Jackowiak */ @Startup @Singleton public class MultipleScheduleTimerBean { @Inject Event<Ping> pingEvent; @Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer") public void fastAutomaticallyScheduled(Timer timer) { fireEvent(timer); } @Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer") public void slowlyAutomaticallyScheduled(Timer timer) { fireEvent(timer); } private void fireEvent(Timer timer) { pingEvent.fire(new Ping(timer.getInfo().toString())); } }
/** * @author Arun Gupta */ @Startup @Singleton public class MySingletonBeanManagedConcurrency { volatile StringBuilder builder; @PostConstruct private void postConstruct() { builder = new StringBuilder(); System.out.println("postConstruct"); } public String readSomething() { return "current timestamp: " + new Date(); } public String writeSomething(String something) { synchronized (builder) { builder.append(something); } return builder.toString() + " : " + new Date(); } }
/** * @author Arun Gupta */ @Startup @Singleton public class MySingleton { StringBuilder builder; @PostConstruct private void postConstruct() { System.out.println("postConstruct"); builder = new StringBuilder(); } @Lock(LockType.READ) public String readSomething() { return "current timestamp: " + new Date(); } @Lock(LockType.WRITE) public String writeSomething(String something) { builder.append(something); return builder.toString() + " : " + new Date(); } }
@Singleton public class ProgrammaticTimerBean {
@Singleton @Startup public class BeanRoot implements BeanRootInterface {
@Named("SingletonBeanDelegate") @Singleton public class SingletonBeanDelegate implements JavaDelegate { public void execute(DelegateExecution execution) throws Exception { execution.setVariable(SingletonBeanDelegate.class.getName(), true); } }
@Singleton public static class AnEJB { @PersistenceContext(unitName = "homEE") private EntityManager em; // just for the tx context public void persist(Object o) { em.persist(o); } } }
/** * A SingletonBean with a local business interface * * @author Daniel Meyer * */ @Singleton @Local(BusinessInterface.class) public class LocalSingletonBean implements BusinessInterface { public boolean doBusiness() { return true; } }
/** * A SingletonBean with a remote business interface * * @author Daniel Meyer * */ @Singleton @Remote(BusinessInterface.class) public class RemoteSingletonBean implements BusinessInterface { public boolean doBusiness() { return true; } }
/** * @author Tassilo Weidner */ @Singleton @Startup @ConcurrencyManagement(ConcurrencyManagementType.BEAN) @TransactionAttribute(TransactionAttributeType.REQUIRED) @ProcessApplication(deploymentDescriptors = {"deployment-descriptor-with-custom-filename.xml"}) @Local(ProcessApplicationInterface.class) public class AnnotatedEjbPa extends EjbProcessApplication { @PostConstruct public void start() { deploy(); } @PreDestroy public void stop() { undeploy(); } }
/** * Custom {@link EjbProcessApplication} with PA lifecycle callbacks * * @author Daniel Meyer * */ @Singleton @Startup @ConcurrencyManagement(ConcurrencyManagementType.BEAN) @TransactionAttribute(TransactionAttributeType.REQUIRED) @ProcessApplication @Local(ProcessApplicationInterface.class) public class CustomEjbProcessApplication extends EjbProcessApplication { @PostDeploy public void postDeploy(ProcessEngine processEngine) { Assert.assertNotNull(processEngine); } @PreUndeploy public void preUnDeploy(ProcessEngine processEngine) { Assert.assertNotNull(processEngine); } }