Source code: org/activemq/transport/reliable/ReliableTransportChannelFactory.java
1 /**
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * 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 **/
18
19 package org.activemq.transport.reliable;
20
21 import org.activemq.io.WireFormat;
22 import org.activemq.transport.TransportChannel;
23 import org.activemq.transport.composite.CompositeTransportChannelFactory;
24 import org.activemq.util.JMSExceptionHelper;
25
26 import javax.jms.JMSException;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32
33 /**
34 * A Reliable implementation of a TransportChannelFactory
35 *
36 * @version $Revision: 1.1.1.1 $
37 */
38 public class ReliableTransportChannelFactory extends CompositeTransportChannelFactory {
39
40 /**
41 * Create a TransportChannel
42 *
43 * @param wireFormat - the on-the-wire marshaller
44 * @param remoteLocation - location to bind to
45 * @return a reliable transport channel
46 * @throws JMSException if an error occurs
47 */
48 public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
49 try {
50 List uris = new ArrayList();
51 String text = parseURIs(uris, remoteLocation);
52 uris = randomizeURIs(uris);
53
54 ReliableTransportChannel channel = new ReliableTransportChannel(wireFormat, uris);
55 channel = (ReliableTransportChannel) populateProperties(channel, text);
56
57 // Using singleton pattern since this factory isn't a singleton itself
58 KeepAliveDaemon daemon = KeepAliveDaemon.getInstance();
59 daemon.addMonitoredChannel(channel);
60 daemon.start();
61
62 return channel;
63 }
64 catch (URISyntaxException e) {
65 throw JMSExceptionHelper.newJMSException("Can't parse list of URIs for: " + remoteLocation + ". Reason: "
66 + e, e);
67 }
68 }
69
70 /**
71 * @param uris - the URIs to randomize
72 * @return randomized array
73 */
74 protected List randomizeURIs(List uris) {
75 if (!uris.isEmpty()) {
76 int size = uris.size();
77 Object[] result = new Object[size];
78 SMLCGRandom random = new SMLCGRandom();
79 int startIndex = (int) (random.nextDouble() * (size + 1));
80 int count = 0;
81 for (int i = startIndex; i < size; i++) {
82 result[count++] = uris.get(i);
83 }
84 for (int i = 0; i < startIndex; i++) {
85 result[count++] = uris.get(i);
86 }
87 return new ArrayList(Arrays.asList(result));
88 }
89 return uris;
90 }
91 }