View Javadoc
1   /*
2    * Copyright 2018 the original author or authors.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.openehealth.ipf.commons.audit.queue;
17  
18  import org.openehealth.ipf.commons.audit.AuditContext;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  import javax.jms.*;
23  
24  import static java.util.Objects.requireNonNull;
25  
26  /**
27   * Message Queue that sends off audit messages into a JMS queue. It is strongly recommended
28   * that the connection factory implements a pool or caches connections for performance reasons.
29   * Use an instance of {@link JmsAuditMessageListener} to asynchronously receive the audit
30   * messages and send them off to a respository.
31   *
32   * @author Dmytro Rud
33   * @author Christian Ohr
34   * @since 3.5
35   *
36   * @see JmsAuditMessageListener
37   */
38  public class JmsAuditMessageQueue extends AbstractAuditMessageQueue {
39  
40      private static final Logger LOG = LoggerFactory.getLogger(JmsAuditMessageQueue.class);
41  
42      private final ConnectionFactory connectionFactory;
43      private final String queueName;
44      private final String userName;
45      private final String password;
46  
47      /**
48       * @param connectionFactory JMS connection factory
49       * @param queueName         JMS destination of ATNA messages
50       * @param userName          user name for JMS authentication
51       * @param password          user password for JMS authentication
52       * @throws JMSException if no connection could be established
53       */
54      public JmsAuditMessageQueue(ConnectionFactory connectionFactory, String queueName,
55                                  String userName, String password) {
56          this.connectionFactory = requireNonNull(connectionFactory, "ConnectionFactory must not be null");
57          this.queueName = queueName != null ? queueName : "atna-audit";
58          this.userName = userName;
59          this.password = password;
60      }
61  
62  
63      @Override
64      protected void handle(AuditContext auditContext, String... auditRecords) {
65          try {
66              Connection connection = connectionFactory.createConnection(userName, password);
67              connection.start();
68              try {
69                  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
70                  Queue queue = session.createQueue(queueName);
71                  MessageProducer producer = session.createProducer(queue);
72                  for (String auditMessage : auditRecords) {
73                      TextMessage message = session.createTextMessage(auditMessage);
74                      producer.send(message);
75                  }
76              } finally {
77                  if (connection != null) connection.close();
78              }
79          } catch (Exception e) {
80              auditContext.getAuditExceptionHandler().handleException(auditContext, e, auditRecords);
81          }
82      }
83  
84      @Override
85      public void flush() {
86          // nop
87      }
88  
89  }