|
activemq-cpp-3.4.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 Pointer<wireformat::WireFormat> getWireFormat() const; 00149 00150 virtual void setWireFormat( const Pointer<wireformat::WireFormat>& wireFormat AMQCPP_UNUSED ) {} 00151 00152 virtual void setTransportListener( TransportListener* listener ); 00153 00154 virtual TransportListener* getTransportListener() const; 00155 00156 virtual bool isFaultTolerant() const { 00157 return true; 00158 } 00159 00160 virtual bool isConnected() const; 00161 00162 virtual bool isClosed() const; 00163 00164 bool isInitialized() const; 00165 00166 void setInitialized( bool value ); 00167 00168 virtual Transport* narrow( const std::type_info& typeId ); 00169 00170 virtual std::string getRemoteAddress() const; 00171 00172 virtual void reconnect( const decaf::net::URI& uri ); 00173 00174 virtual void updateURIs( bool rebalance, const decaf::util::List<decaf::net::URI>& uris ); 00175 00176 public: // CompositeTask Methods. 00177 00182 virtual bool isPending() const; 00183 00191 virtual bool iterate(); 00192 00193 public: // FailoverTransport Property Getters / Setters 00194 00195 long long getTimeout() const; 00196 00197 void setTimeout( long long value ); 00198 00199 long long getInitialReconnectDelay() const; 00200 00201 void setInitialReconnectDelay( long long value ); 00202 00203 long long getMaxReconnectDelay() const; 00204 00205 void setMaxReconnectDelay( long long value ); 00206 00207 long long getBackOffMultiplier() const; 00208 00209 void setBackOffMultiplier( long long value ); 00210 00211 bool isUseExponentialBackOff() const; 00212 00213 void setUseExponentialBackOff( bool value ); 00214 00215 bool isRandomize() const; 00216 00217 void setRandomize( bool value ); 00218 00219 int getMaxReconnectAttempts() const; 00220 00221 void setMaxReconnectAttempts( int value ); 00222 00223 int getStartupMaxReconnectAttempts() const; 00224 00225 void setStartupMaxReconnectAttempts( int value ); 00226 00227 long long getReconnectDelay() const; 00228 00229 void setReconnectDelay( long long value ); 00230 00231 bool isBackup() const; 00232 00233 void setBackup( bool value ); 00234 00235 int getBackupPoolSize() const; 00236 00237 void setBackupPoolSize( int value ); 00238 00239 bool isTrackMessages() const; 00240 00241 void setTrackMessages( bool value ); 00242 00243 bool isTrackTransactionProducers() const; 00244 00245 void setTrackTransactionProducers( bool value ); 00246 00247 int getMaxCacheSize() const; 00248 00249 void setMaxCacheSize( int value ); 00250 00251 bool isReconnectSupported() const; 00252 00253 void setReconnectSupported( bool value ); 00254 00255 bool isUpdateURIsSupported() const; 00256 00257 void setUpdateURIsSupported( bool value ); 00258 00259 void setConnectionInterruptProcessingComplete( const Pointer<commands::ConnectionId>& connectionId ); 00260 00261 protected: 00262 00272 void restoreTransport( const Pointer<Transport>& transport ); 00273 00279 void handleTransportFailure( const decaf::lang::Exception& error ); 00280 00289 void handleConnectionControl( const Pointer<Command>& control ); 00290 00291 private: 00292 00301 Pointer<Transport> createTransport( const URI& location ) const; 00302 00303 void processNewTransports( bool rebalance, std::string newTransports ); 00304 00305 void processResponse(const Pointer<Response>& response); 00306 00307 }; 00308 00309 }}} 00310 00311 #endif /* _ACTIVE_TRANSPORT_FAILOVER_FAILOVERTRANSPORT_H_ */