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.xa;
18  
19  import javax.ejb.ActivationConfigProperty;
20  import javax.ejb.MessageDriven;
21  import javax.inject.Inject;
22  import javax.jms.JMSException;
23  import javax.jms.Message;
24  import javax.jms.MessageListener;
25  import javax.jms.TextMessage;
26  import java.util.logging.Logger;
27  
28  /**
29   * <p>
30   * A Message Driven Bean that asynchronously receives and processes
31   * messages of the form key=value and updates a database table with
32   * those values using the services of an injected bean.
33   * </p>
34   *
35   * @author Serge Pagop (spagop@redhat.com)
36   * @author Mike Musgrove (mmusgrov@redhat.com)
37   *
38   */
39  @MessageDriven(name = "DbUpdaterMDB", activationConfig = {
40          @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
41          @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/jta-crash-rec-quickstart"),
42          @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
43  public class DbUpdaterMDB implements MessageListener {
44      
45      private final static Logger LOGGER = Logger.getLogger(DbUpdaterMDB.class.getName());
46      
47      // A helper service for persisting key value pairs
48      @Inject
49      private XAService xaService;
50  
51      public void onMessage(Message rcvMessage) {
52          LOGGER.info("Received Message: " + rcvMessage.toString());
53  
54          try {
55              if (rcvMessage instanceof TextMessage) {
56                  TextMessage msg = (TextMessage) rcvMessage;
57                  String sm = msg.getText();
58                  String[] kvPair = sm.split("=");
59  
60                  // only process messages of the form key=value
61                  if (kvPair.length == 2 && kvPair[0].length() != 0) {
62                      xaService.modifyKeyValueTable(false, kvPair[0], kvPair[1] + " updated via JMS");
63                      LOGGER.info("JTA Crash Record Quickstart: key value pair updated via JMS");
64                  }
65              } else {
66                  LOGGER.warning("JTA Crash Record Quickstart: Unexpected message. Type: " + rcvMessage.getClass().getName());
67              }
68          } catch (JMSException e) {
69              throw new RuntimeException(e);
70          }
71      }
72  }