001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.activemq.spring; 019 020import java.util.ArrayList; 021import java.util.List; 022 023import org.springframework.beans.factory.FactoryBean; 024 025/** 026 * A helper class for creating a failover configured {@link ActiveMQConnectionFactory} 027 * which supports one or more TCP based hostname/ports which can all be configured in a 028 * consistent way without too much URL hacking. 029 * 030 * 031 */ 032public class ActiveMQConnectionFactoryFactoryBean implements FactoryBean { 033 private List<String> tcpHostAndPorts = new ArrayList<String>(); 034 035 // tcp properties 036 private Long maxInactivityDuration; 037 private String tcpProperties; 038 039 // failover properties 040 private Long maxReconnectDelay; 041 private String failoverProperties; 042 043 public Object getObject() throws Exception { 044 ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory(); 045 String brokerURL = getBrokerURL(); 046 answer.setBrokerURL(brokerURL); 047 return answer; 048 } 049 050 public String getBrokerURL() { 051 StringBuffer buffer = new StringBuffer("failover:("); 052 int counter = 0; 053 for (String tcpHostAndPort : tcpHostAndPorts) { 054 if (counter++ > 0) { 055 buffer.append(","); 056 } 057 buffer.append(createTcpHostAndPortUrl(tcpHostAndPort)); 058 } 059 buffer.append(")"); 060 061 List<String> parameters = new ArrayList<String>(); 062 if (maxReconnectDelay != null) { 063 parameters.add("maxReconnectDelay=" + maxReconnectDelay); 064 } 065 if (notEmpty(failoverProperties)) { 066 parameters.add(failoverProperties); 067 } 068 buffer.append(asQueryString(parameters)); 069 return buffer.toString(); 070 } 071 072 public Class getObjectType() { 073 return ActiveMQConnectionFactory.class; 074 } 075 076 public boolean isSingleton() { 077 return true; 078 } 079 080 // Properties 081 //------------------------------------------------------------------------- 082 083 public List<String> getTcpHostAndPorts() { 084 return tcpHostAndPorts; 085 } 086 087 public void setTcpHostAndPorts(List<String> tcpHostAndPorts) { 088 this.tcpHostAndPorts = tcpHostAndPorts; 089 } 090 091 public void setTcpHostAndPort(String tcpHostAndPort) { 092 tcpHostAndPorts = new ArrayList<String>(); 093 tcpHostAndPorts.add(tcpHostAndPort); 094 } 095 096 public Long getMaxInactivityDuration() { 097 return maxInactivityDuration; 098 } 099 100 public void setMaxInactivityDuration(Long maxInactivityDuration) { 101 this.maxInactivityDuration = maxInactivityDuration; 102 } 103 104 public String getTcpProperties() { 105 return tcpProperties; 106 } 107 108 public void setTcpProperties(String tcpProperties) { 109 this.tcpProperties = tcpProperties; 110 } 111 112 public Long getMaxReconnectDelay() { 113 return maxReconnectDelay; 114 } 115 116 public void setMaxReconnectDelay(Long maxReconnectDelay) { 117 this.maxReconnectDelay = maxReconnectDelay; 118 } 119 120 public String getFailoverProperties() { 121 return failoverProperties; 122 } 123 124 public void setFailoverProperties(String failoverProperties) { 125 this.failoverProperties = failoverProperties; 126 } 127 128 // Implementation methods 129 //------------------------------------------------------------------------- 130 131 /** 132 * Turns a list of query string key=value strings into a query URL string 133 * of the form "?a=x&b=y" 134 */ 135 protected String asQueryString(List<String> parameters) { 136 int size = parameters.size(); 137 if (size < 1) { 138 return ""; 139 } 140 else { 141 StringBuffer buffer = new StringBuffer("?"); 142 buffer.append(parameters.get(0)); 143 for (int i = 1; i < size; i++) { 144 buffer.append("&"); 145 buffer.append(parameters.get(i)); 146 } 147 return buffer.toString(); 148 } 149 } 150 151 /** 152 * Allows us to add any TCP specific URI configurations 153 */ 154 protected String createTcpHostAndPortUrl(String tcpHostAndPort) { 155 List<String> parameters = new ArrayList<String>(); 156 if (maxInactivityDuration != null) { 157 parameters.add("wireFormat.maxInactivityDuration=" + maxInactivityDuration); 158 } 159 if (notEmpty(tcpProperties)) { 160 parameters.add(tcpProperties); 161 } 162 return tcpHostAndPort + asQueryString(parameters); 163 } 164 165 166 protected boolean notEmpty(String text) { 167 return text != null && text.length() > 0; 168 } 169 170}