#ifndef _THRIFT_TQUEUINGASYNCPROCESSOR_H_ #define _THRIFT_TQUEUINGASYNCPROCESSOR_H_ 1 #include #include #include "thrift/lib/cpp/TProcessor.h" #include "thrift/lib/cpp/async/TAsyncProcessor.h" #include "thrift/lib/cpp/async/TEventTask.h" #include "thrift/lib/cpp/concurrency/Exception.h" namespace apache { namespace thrift { namespace async { /** * Adapter to allow a TProcessor to be used as a TAsyncProcessor. * * Note: this is not intended for use outside of TEventConnection since the * callback mechanism used in TEventTask will invoke handleAsyncTaskComplete() * regardless of what is passed in as the cob. * * Uses a per-server task queue for all calls. */ class TQueuingAsyncProcessor : public TAsyncProcessor { public: TQueuingAsyncProcessor( boost::shared_ptr processor, boost::shared_ptr threadManager, int64_t taskExpireTime, TEventConnection* connection) : processor_(processor) , threadManager_(threadManager) , taskExpireTime_(taskExpireTime) , connection_(connection) {} virtual void process( std::tr1::function cob, boost::shared_ptr in, boost::shared_ptr out, TConnectionContext* context) { boost::shared_ptr task = boost::shared_ptr( new TEventTask(connection_)); try { threadManager_->add(task, 0LL, taskExpireTime_); } catch (apache::thrift::concurrency::IllegalStateException & ise) { T_ERROR("IllegalStateException: TQueuingAsyncProcessor::process() %s", ise.what()); // no task will be making a callback return cob(false); } } private: boost::shared_ptr processor_; /// For processing via thread pool boost::shared_ptr threadManager_; /// Time in milliseconds before an unperformed task expires (0 == infinite). int64_t taskExpireTime_; /// The worker that started us TEventConnection* connection_; }; }}} // apache::thrift::async #endif // #ifndef _THRIFT_TQUEUINGASYNCPROCESSOR_H_