01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
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.apache.catalina.tribes.transport.nio;
18:
19: import java.io.IOException;
20:
21: import org.apache.catalina.tribes.ChannelException;
22: import org.apache.catalina.tribes.ChannelMessage;
23: import org.apache.catalina.tribes.Member;
24: import org.apache.catalina.tribes.transport.DataSender;
25: import org.apache.catalina.tribes.transport.MultiPointSender;
26: import org.apache.catalina.tribes.transport.PooledSender;
27:
28: /**
29: * <p>Title: </p>
30: *
31: * <p>Description: </p>
32: *
33: * <p>Company: </p>
34: *
35: * @author not attributable
36: * @version 1.0
37: */
38: public class PooledParallelSender extends PooledSender implements
39: MultiPointSender {
40: protected boolean connected = true;
41:
42: public PooledParallelSender() {
43: super ();
44: }
45:
46: public void sendMessage(Member[] destination, ChannelMessage message)
47: throws ChannelException {
48: if (!connected)
49: throw new ChannelException("Sender not connected.");
50: ParallelNioSender sender = (ParallelNioSender) getSender();
51: if (sender == null) {
52: ChannelException cx = new ChannelException(
53: "Unable to retrieve a data sender, time out error.");
54: for (int i = 0; i < destination.length; i++)
55: cx
56: .addFaultyMember(
57: destination[i],
58: new NullPointerException(
59: "Unable to retrieve a sender from the sender pool"));
60: throw cx;
61: } else {
62: try {
63: sender.sendMessage(destination, message);
64: sender.keepalive();
65: } finally {
66: if (!connected)
67: disconnect();
68: returnSender(sender);
69: }
70: }
71: }
72:
73: public DataSender getNewDataSender() {
74: try {
75: ParallelNioSender sender = new ParallelNioSender();
76: sender.transferProperties(this , sender);
77: return sender;
78: } catch (IOException x) {
79: throw new RuntimeException("Unable to open NIO selector.",
80: x);
81: }
82: }
83:
84: public synchronized void disconnect() {
85: this .connected = false;
86: super .disconnect();
87: }
88:
89: public synchronized void connect() throws IOException {
90: this .connected = true;
91: super.connect();
92: }
93:
94: }
|