001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. 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 package org.apache.activemq.transport.discovery.masterslave;
018
019 import java.net.URI;
020 import org.apache.activemq.transport.discovery.simple.SimpleDiscoveryAgent;
021 import org.slf4j.Logger;
022 import org.slf4j.LoggerFactory;
023
024 /**
025 * A static DiscoveryAgent that supports connecting to a Master / Slave tuple
026 * of brokers.
027 */
028 public class MasterSlaveDiscoveryAgent extends SimpleDiscoveryAgent {
029
030 private final static Logger LOG = LoggerFactory.getLogger(MasterSlaveDiscoveryAgent.class);
031
032 private String[] msServices = new String[]{};
033
034 @Override
035 public String[] getServices() {
036 return msServices;
037 }
038
039 @Override
040 public void setServices(String services) {
041 this.msServices = services.split(",");
042 configureServices();
043 }
044
045 @Override
046 public void setServices(String services[]) {
047 this.msServices = services;
048 configureServices();
049 }
050
051 @Override
052 public void setServices(URI services[]) {
053 this.msServices = new String[services.length];
054 for (int i = 0; i < services.length; i++) {
055 this.msServices[i] = services[i].toString();
056 }
057 configureServices();
058 }
059
060 protected void configureServices() {
061 if ((msServices == null) || (msServices.length < 2)) {
062 LOG.error("masterSlave requires at least 2 URIs");
063 msServices = new String[]{};
064 throw new IllegalArgumentException("Expecting at least 2 arguments");
065 }
066
067 StringBuffer buf = new StringBuffer();
068
069 buf.append("failover:(");
070
071 for (int i = 0; i < (msServices.length - 1); i++) {
072 buf.append(msServices[i]);
073 buf.append(',');
074 }
075 buf.append(msServices[msServices.length - 1]);
076
077 buf.append(")?randomize=false&maxReconnectAttempts=0");
078
079 super.setServices(new String[]{buf.toString()});
080 }
081
082 }