View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.ejb.asynchronous;
18  
19  import java.util.Date;
20  import java.util.concurrent.Future;
21  import java.util.logging.Logger;
22  
23  import javax.ejb.AsyncResult;
24  import javax.ejb.Asynchronous;
25  import javax.ejb.Stateless;
26  
27  /**
28   * <p>
29   * A simple SLSB to demonstrate the asynchronous EJB call.
30   * </p>
31   * <p>
32   * Here the methods are annotated but it is also possible to add the annotation at class level, in that case all methods of this
33   * bean are asynchrounous.
34   * </p>
35   * 
36   * @author <a href="mailto:wfink@redhat.com">Wolf-Dieter Fink</a>
37   */
38  @Stateless
39  public class AsynchronousAccessBean implements AsynchronousAccess, AnotherAsynchronousAccess {
40      private static final Logger LOGGER = Logger.getLogger(AsynchronousAccessBean.class.getName());
41  
42      @Asynchronous
43      @Override
44      public void fireAndForget(long sleepTime) {
45          LOGGER.info("'fireAndForget' Will wait for " + sleepTime + "ms");
46          try {
47              Thread.sleep(sleepTime);
48          } catch (InterruptedException e) {
49          }
50          LOGGER.info("action 'fireAndForget' finished");
51      }
52  
53      @Asynchronous
54      @Override
55      public Future<String> longerRunning(long sleepTime) {
56          LOGGER.info("Will wait for " + sleepTime + "ms");
57          try {
58              Thread.sleep(sleepTime);
59          } catch (InterruptedException e) {
60          }
61          LOGGER.info("returning the result");
62          return new AsyncResult<String>("returning at " + new Date() + ", duration was " + sleepTime + "ms");
63      }
64  
65      @Asynchronous
66      @Override
67      public Future<String> failure() throws IllegalAccessException {
68          try {
69              Thread.sleep(400);
70          } catch (InterruptedException e) {
71          }
72          throw new IllegalAccessException("Asynchrounous fail demonstration");
73      }
74  
75      @Override
76      public Future<String> interfaceAsync(long sleepTime) {
77          LOGGER.info("Will wait for " + sleepTime + "ms");
78          try {
79              Thread.sleep(sleepTime);
80          } catch (InterruptedException e) {
81          }
82          LOGGER.info("returning the result");
83          return new AsyncResult<String>("returning at " + new Date() + ", duration was " + sleepTime + "ms");
84      }
85  }