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.wsat.simple;
18  
19  import com.arjuna.mw.wst11.TransactionManager;
20  import com.arjuna.mw.wst11.TransactionManagerFactory;
21  import com.arjuna.mw.wst11.UserTransactionFactory;
22  
23  import org.jboss.as.quickstarts.wsat.simple.jaxws.RestaurantServiceAT;
24  
25  import javax.jws.HandlerChain;
26  import javax.jws.WebMethod;
27  import javax.jws.WebService;
28  import javax.jws.soap.SOAPBinding;
29  import javax.servlet.annotation.WebServlet;
30  import java.util.UUID;
31  
32  /**
33   * An adapter class that exposes the RestaurantManager business API as a transactional Web Service.
34   * 
35   * @author paul.robinson@redhat.com, 2012-01-04
36   * 
37   */
38  @WebService(serviceName = "RestaurantServiceATService", portName = "RestaurantServiceAT", name = "RestaurantServiceAT", targetNamespace = "http://www.jboss.org/jboss-jdf/jboss-as-quickstart/wsat/simple/Restaurant")
39  @HandlerChain(file = "/context-handlers.xml", name = "Context Handlers")
40  @SOAPBinding(style = SOAPBinding.Style.RPC)
41  @WebServlet("/RestaurantServiceAT")
42  public class RestaurantServiceATImpl implements RestaurantServiceAT {
43  
44      private MockRestaurantManager mockRestaurantManager = MockRestaurantManager.getSingletonInstance();
45  
46      /**
47       * Book a number of seats in the restaurant. Enrols a Participant, then passes the call through to the business logic.
48       */
49      @WebMethod
50      public void makeBooking() throws RestaurantException {
51  
52          System.out.println("[SERVICE] Restaurant service invoked to make a booking");
53          String transactionId;
54          try {
55              // get the transaction ID associated with this thread
56              transactionId = UserTransactionFactory.userTransaction().toString();
57  
58              // enlist the Participant for this service:
59              RestaurantParticipant restaurantParticipant = new RestaurantParticipant(transactionId);
60              TransactionManager transactionManager = TransactionManagerFactory.transactionManager();
61              System.out.println("[SERVICE] Enlisting a Durable2PC participant into the AT");
62              transactionManager.enlistForDurableTwoPhase(restaurantParticipant, "restaurantServiceAT:" + UUID.randomUUID());
63          } catch (Exception e) {
64              throw new RestaurantException("Error when enlisting participant", e);
65          }
66  
67          // invoke the backend business logic:
68          System.out.println("[SERVICE] Invoking the back-end business logic");
69          mockRestaurantManager.makeBooking(transactionId);
70      }
71  
72      /**
73       * obtain the number of existing bookings
74       * 
75       * @return the number of current bookings
76       */
77      @Override
78      public int getBookingCount() {
79          return mockRestaurantManager.getBookingCount();
80      }
81  
82      /**
83       * Reset the booking count to zero
84       * 
85       * Note: To simplify this example, this method is not part of the compensation logic, so will not be undone if the AT is
86       * compensated. It can also be invoked outside of an active AT.
87       */
88      @Override
89      public void reset() {
90          mockRestaurantManager.reset();
91      }
92  }