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.ArrayList;
20  import java.util.Collection;
21  import java.util.concurrent.ExecutionException;
22  import java.util.concurrent.Future;
23  import java.util.logging.Level;
24  import java.util.logging.Logger;
25  
26  import javax.ejb.EJB;
27  import javax.ejb.Stateless;
28  
29  
30  /**
31   * Stateless session bean implementation to demonstrate how to invoke asynchronous methods to parallelize different actions to
32   * minimize the duration time of client invocation.
33   * 
34   * @author <a href="mailto:wfink@redhat.com">Wolf-Dieter Fink</a>
35   */
36  @Stateless
37  public class ParallelAccessBean implements ParallelAccess {
38      private static final Logger LOGGER = Logger.getLogger(ParallelAccessBean.class.getName());
39  
40      @EJB
41      private AsynchronousAccess asyncBean;
42      @EJB
43      private AnotherAsynchronousAccess anotherBean;
44  
45      @Override
46      public Collection<String> invokeAsyncParallel() {
47          ArrayList<String> results = new ArrayList<String>();
48          Future<String> call1 = asyncBean.longerRunning(5000);
49          Future<String> call2 = asyncBean.longerRunning(3000);
50  
51          try {
52              results.add(call1.get());
53              results.add(call2.get());
54          } catch (InterruptedException e) {
55              LOGGER.log(Level.SEVERE, "Asynchronous call was interrupted!", e);
56              throw new RuntimeException("Asynchronous call was interrupted!", e);
57          } catch (ExecutionException e) {
58              LOGGER.log(Level.SEVERE, "Asynchronous call failed!", e);
59              throw new RuntimeException("Asynchronous call failed!", e);
60          }
61          return results;
62      }
63  
64      @Override
65      public void callInterfaceAnnotatedMethod() {
66          // call the example method with interface annotation
67          final Future<String> call1 = anotherBean.interfaceAsync(500);
68  
69          try {
70              // wait for the result and log it
71              LOGGER.info("CALL result " + call1.get());
72          } catch (Exception e) {
73              throw new RuntimeException("Asynchronous (interface annotated) method can not be called!", e);
74          }
75      }
76  }