001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 019 package org.activemq.transport.reliable; 020 021 import org.activemq.io.WireFormat; 022 import org.activemq.transport.TransportChannel; 023 import org.activemq.transport.composite.CompositeTransportChannelFactory; 024 import org.activemq.util.JMSExceptionHelper; 025 026 import javax.jms.JMSException; 027 import java.net.URI; 028 import java.net.URISyntaxException; 029 import java.util.ArrayList; 030 import java.util.Arrays; 031 import java.util.List; 032 033 /** 034 * A Reliable implementation of a TransportChannelFactory 035 * 036 * @version $Revision: 1.1.1.1 $ 037 */ 038 public class ReliableTransportChannelFactory extends CompositeTransportChannelFactory { 039 040 /** 041 * Create a TransportChannel 042 * 043 * @param wireFormat - the on-the-wire marshaller 044 * @param remoteLocation - location to bind to 045 * @return a reliable transport channel 046 * @throws JMSException if an error occurs 047 */ 048 public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException { 049 try { 050 List uris = new ArrayList(); 051 String text = parseURIs(uris, remoteLocation); 052 uris = randomizeURIs(uris); 053 054 ReliableTransportChannel channel = new ReliableTransportChannel(wireFormat, uris); 055 channel = (ReliableTransportChannel) populateProperties(channel, text); 056 057 // Using singleton pattern since this factory isn't a singleton itself 058 KeepAliveDaemon daemon = KeepAliveDaemon.getInstance(); 059 daemon.addMonitoredChannel(channel); 060 daemon.start(); 061 062 return channel; 063 } 064 catch (URISyntaxException e) { 065 throw JMSExceptionHelper.newJMSException("Can't parse list of URIs for: " + remoteLocation + ". Reason: " 066 + e, e); 067 } 068 } 069 070 /** 071 * @param uris - the URIs to randomize 072 * @return randomized array 073 */ 074 protected List randomizeURIs(List uris) { 075 if (!uris.isEmpty()) { 076 int size = uris.size(); 077 Object[] result = new Object[size]; 078 SMLCGRandom random = new SMLCGRandom(); 079 int startIndex = (int) (random.nextDouble() * (size + 1)); 080 int count = 0; 081 for (int i = startIndex; i < size; i++) { 082 result[count++] = uris.get(i); 083 } 084 for (int i = 0; i < startIndex; i++) { 085 result[count++] = uris.get(i); 086 } 087 return new ArrayList(Arrays.asList(result)); 088 } 089 return uris; 090 } 091 }