View Javadoc
1   /*
2    * Copyright 2015 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.platform.camel.ihe.mllp.core;
17  
18  import lombok.experimental.Delegate;
19  import org.apache.camel.component.mina2.Mina2Consumer;
20  import org.apache.camel.impl.DefaultConsumer;
21  import org.apache.mina.core.future.CloseFuture;
22  import org.apache.mina.core.service.IoAcceptor;
23  import org.apache.mina.core.session.IoSession;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * MllpConsumer wraps a Mina2Consumer for having a hook to shutdown some Mina
29   * resources when the consumer is closing
30   */
31  public class MllpConsumer extends DefaultConsumer {
32  
33      private static final Logger LOG = LoggerFactory.getLogger(MllpConsumer.class);
34  
35      // The reason for this interface is to convince the Delegate annotation to *not* delegate
36      // the stop method. Weird API, really.
37      private interface DoStop {
38          @SuppressWarnings("unused")
39          void stop() throws Exception;
40      }
41  
42      @Delegate(excludes = DoStop.class)
43      private final Mina2Consumer consumer;
44  
45      MllpConsumer(Mina2Consumer consumer) {
46          // Everything will be delegated
47          super(consumer.getEndpoint(), consumer.getProcessor());
48          this.consumer = consumer;
49      }
50  
51      @Override
52      protected void handleException(String message, Throwable t) {
53          super.handleException(message, t);
54      }
55  
56      @Override
57      protected void handleException(Throwable t) {
58          super.handleException(t);
59      }
60  
61      @Override
62      public void stop() throws Exception {
63          super.stop();
64      }
65  
66      @Override
67      protected void doStop() throws Exception {
68          super.doStop();
69          IoAcceptor ioAcceptor = getAcceptor();
70          if (ioAcceptor != null) {
71              for (IoSession ss : ioAcceptor.getManagedSessions().values()) {
72                  CloseFuture future = ss.closeNow();
73                  if (!future.awaitUninterruptibly(1000)) {
74                      LOG.warn("Could not close IoSession, consumer may hang");
75                  }
76              }
77              ioAcceptor.unbind();
78              ioAcceptor.dispose(false);
79          }
80      }
81  
82  }