|
activemq-cpp-3.3.0
|
00001 /* 00002 * Licensed to the Apache Software Foundation (ASF) under one or more 00003 * contributor license agreements. See the NOTICE file distributed with 00004 * this work for additional information regarding copyright ownership. 00005 * The ASF licenses this file to You under the Apache License, Version 2.0 00006 * (the "License"); you may not use this file except in compliance with 00007 * the License. You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #ifndef _ACTIVE_TRANSPORT_FAILOVER_FAILOVERTRANSPORT_H_ 00019 #define _ACTIVE_TRANSPORT_FAILOVER_FAILOVERTRANSPORT_H_ 00020 00021 #include <activemq/util/Config.h> 00022 00023 #include <activemq/commands/Command.h> 00024 #include <activemq/commands/ConnectionId.h> 00025 #include <activemq/threads/TaskRunner.h> 00026 #include <activemq/threads/CompositeTaskRunner.h> 00027 #include <activemq/state/ConnectionStateTracker.h> 00028 #include <activemq/transport/CompositeTransport.h> 00029 #include <activemq/transport/failover/BackupTransportPool.h> 00030 #include <activemq/transport/failover/CloseTransportsTask.h> 00031 #include <activemq/transport/failover/FailoverTransportListener.h> 00032 #include <activemq/transport/failover/URIPool.h> 00033 #include <activemq/wireformat/WireFormat.h> 00034 00035 #include <decaf/util/LinkedList.h> 00036 #include <decaf/util/StlMap.h> 00037 #include <decaf/util/Properties.h> 00038 #include <decaf/util/concurrent/Mutex.h> 00039 #include <decaf/util/concurrent/atomic/AtomicReference.h> 00040 #include <decaf/net/URI.h> 00041 #include <decaf/io/IOException.h> 00042 00043 namespace activemq { 00044 namespace transport { 00045 namespace failover { 00046 00047 using namespace decaf::lang; 00048 using decaf::net::URI; 00049 using namespace decaf::util; 00050 using namespace activemq::threads; 00051 using activemq::commands::Command; 00052 using activemq::commands::Response; 00053 00054 class AMQCPP_API FailoverTransport : public CompositeTransport, 00055 public activemq::threads::CompositeTask { 00056 private: 00057 00058 friend class FailoverTransportListener; 00059 00060 bool closed; 00061 bool connected; 00062 bool started; 00063 00064 long long timeout; 00065 long long initialReconnectDelay; 00066 long long maxReconnectDelay; 00067 long long backOffMultiplier; 00068 bool useExponentialBackOff; 00069 bool initialized; 00070 int maxReconnectAttempts; 00071 int startupMaxReconnectAttempts; 00072 int connectFailures; 00073 long long reconnectDelay; 00074 bool trackMessages; 00075 bool trackTransactionProducers; 00076 int maxCacheSize; 00077 bool connectionInterruptProcessingComplete; 00078 bool firstConnection; 00079 bool updateURIsSupported; 00080 bool reconnectSupported; 00081 00082 mutable decaf::util::concurrent::Mutex reconnectMutex; 00083 mutable decaf::util::concurrent::Mutex sleepMutex; 00084 mutable decaf::util::concurrent::Mutex listenerMutex; 00085 00086 state::ConnectionStateTracker stateTracker; 00087 decaf::util::StlMap<int, Pointer<Command> > requestMap; 00088 00089 Pointer<URIPool> uris; 00090 decaf::util::LinkedList<URI> updated; 00091 Pointer<URI> connectedTransportURI; 00092 Pointer<Transport> connectedTransport; 00093 Pointer<Exception> connectionFailure; 00094 Pointer<BackupTransportPool> backups; 00095 Pointer<CloseTransportsTask> closeTask; 00096 Pointer<CompositeTaskRunner> taskRunner; 00097 Pointer<TransportListener> disposedListener; 00098 Pointer<TransportListener> myTransportListener; 00099 TransportListener* transportListener; 00100 00101 private: 00102 00103 FailoverTransport( const FailoverTransport& ); 00104 FailoverTransport& operator= ( const FailoverTransport& ); 00105 00106 public: 00107 00108 FailoverTransport(); 00109 00110 virtual ~FailoverTransport(); 00111 00119 void reconnect( bool rebalance ); 00120 00126 void add( const std::string& uri ); 00127 00128 public: // CompositeTransport methods 00129 00130 virtual void addURI( bool rebalance, const List<URI>& uris ); 00131 00132 virtual void removeURI( bool rebalance, const List<URI>& uris ); 00133 00134 public: // Transport Members 00135 00136 virtual void start(); 00137 00138 virtual void stop(); 00139 00140 virtual void close(); 00141 00142 virtual void oneway( const Pointer<Command>& command ); 00143 00144 virtual Pointer<Response> request( const Pointer<Command>& command ); 00145 00146 virtual Pointer<Response> request( const Pointer<Command>& command, unsigned int timeout ); 00147 00148 virtual void setWireFormat( const Pointer<wireformat::WireFormat>& wireFormat AMQCPP_UNUSED ) {} 00149 00150 virtual void setTransportListener( TransportListener* listener ); 00151 00152 virtual TransportListener* getTransportListener() const; 00153 00154 virtual bool isFaultTolerant() const { 00155 return true; 00156 } 00157 00158 virtual bool isConnected() const; 00159 00160 virtual bool isClosed() const; 00161 00162 bool isInitialized() const; 00163 00164 void setInitialized( bool value ); 00165 00166 virtual Transport* narrow( const std::type_info& typeId ); 00167 00168 virtual std::string getRemoteAddress() const; 00169 00170 virtual void reconnect( const decaf::net::URI& uri ); 00171 00172 virtual void updateURIs( bool rebalance, const decaf::util::List<decaf::net::URI>& uris ); 00173 00174 public: // CompositeTask Methods. 00175 00180 virtual bool isPending() const; 00181 00189 virtual bool iterate(); 00190 00191 public: // FailoverTransport Property Getters / Setters 00192 00193 long long getTimeout() const; 00194 00195 void setTimeout( long long value ); 00196 00197 long long getInitialReconnectDelay() const; 00198 00199 void setInitialReconnectDelay( long long value ); 00200 00201 long long getMaxReconnectDelay() const; 00202 00203 void setMaxReconnectDelay( long long value ); 00204 00205 long long getBackOffMultiplier() const; 00206 00207 void setBackOffMultiplier( long long value ); 00208 00209 bool isUseExponentialBackOff() const; 00210 00211 void setUseExponentialBackOff( bool value ); 00212 00213 bool isRandomize() const; 00214 00215 void setRandomize( bool value ); 00216 00217 int getMaxReconnectAttempts() const; 00218 00219 void setMaxReconnectAttempts( int value ); 00220 00221 int getStartupMaxReconnectAttempts() const; 00222 00223 void setStartupMaxReconnectAttempts( int value ); 00224 00225 long long getReconnectDelay() const; 00226 00227 void setReconnectDelay( long long value ); 00228 00229 bool isBackup() const; 00230 00231 void setBackup( bool value ); 00232 00233 int getBackupPoolSize() const; 00234 00235 void setBackupPoolSize( int value ); 00236 00237 bool isTrackMessages() const; 00238 00239 void setTrackMessages( bool value ); 00240 00241 bool isTrackTransactionProducers() const; 00242 00243 void setTrackTransactionProducers( bool value ); 00244 00245 int getMaxCacheSize() const; 00246 00247 void setMaxCacheSize( int value ); 00248 00249 bool isReconnectSupported() const; 00250 00251 void setReconnectSupported( bool value ); 00252 00253 bool isUpdateURIsSupported() const; 00254 00255 void setUpdateURIsSupported( bool value ); 00256 00257 void setConnectionInterruptProcessingComplete( const Pointer<commands::ConnectionId>& connectionId ); 00258 00259 protected: 00260 00270 void restoreTransport( const Pointer<Transport>& transport ); 00271 00277 void handleTransportFailure( const decaf::lang::Exception& error ); 00278 00287 void handleConnectionControl( const Pointer<Command>& control ); 00288 00289 private: 00290 00299 Pointer<Transport> createTransport( const URI& location ) const; 00300 00301 void processNewTransports( bool rebalance, std::string newTransports ); 00302 00303 void processResponse(const Pointer<Response>& response); 00304 00305 }; 00306 00307 }}} 00308 00309 #endif /* _ACTIVE_TRANSPORT_FAILOVER_FAILOVERTRANSPORT_H_ */