Class: Vertx::Context
- Inherits:
-
Object
- Object
- Vertx::Context
- Defined in:
- /Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb
Overview
When Vert.x provides an event to a handler or calls the start or stop methods of a Nil,
the execution is associated with a Context
.
Usually a context is an *event-loop context* and is tied to a specific event loop thread. So executions for that context always occur on that exact same event loop thread.
In the case of worker verticles and running inline blocking code a worker context will be associated with the execution which will use a thread from the worker thread pool.
When a handler is set by a thread associated with a specific context, the Vert.x will guarantee that when that handler is executed, that execution will be associated with the same context.
If a handler is set by a thread not associated with a context (i.e. a non Vert.x thread). Then a new context will be created for that handler.
In other words, a context is propagated.
This means that when a verticle is deployed, any handlers it sets will be associated with the same context - the context of the verticle.
This means (in the case of a standard verticle) that the verticle code will always be executed with the exact same thread, so you don't have to worry about multi-threaded acccess to the verticle state and you can code your application as single threaded.
This class also allows arbitrary data to be #put and #get on the context so it can be shared easily amongst different handlers of, for example, a verticle instance.
This class also provides #run_on_context which allows an action to be executed asynchronously using the same context.
Constant Summary
- @@j_api_type =
Object.new
Class Method Summary (collapse)
- + (Boolean) accept?(obj)
- + (Object) j_api_type
- + (Object) j_class
-
+ (true, false) isOnEventLoopThread
Is the current thread an event thread? NOTE! This is not always the same as calling #is_event_loop_context.
-
+ (true, false) isOnVertxThread
Is the current thread a Vert.x thread? That's either a worker thread or an event loop thread.
-
+ (true, false) isOnWorkerThread
Is the current thread a worker thread? NOTE! This is not always the same as calling #is_worker_context.
- + (Object) unwrap(obj)
- + (Object) wrap(obj)
Instance Method Summary (collapse)
-
- (Hash{String => Object}) config
If the context is associated with a Verticle deployment, this returns the configuration that was specified when the verticle was deployed.
-
- (String) deploymentID
If the context is associated with a Verticle deployment, this returns the deployment ID of that deployment.
-
- (true, false) isEventLoopContext
Is the current context an event loop context? NOTE! when running blocking code using Vertx#execute_blocking from a standard (not worker) verticle, the context will still an event loop context and this will return true.
-
- (self) exceptionHandler(handler) { ... }
Set an exception handler called when the context runs an action throwing an uncaught throwable.
-
- (void) execute_blocking(*args)
Safely execute some blocking code.
-
- (Object) get(key)
Get some data from the context.
-
- (Fixnum) getInstanceCount
The number of instances of the verticle that were deployed in the deployment (if any) related to this context.
-
- (Object) getLocal(key)
Get some local data from the context.
-
- (::Vertx::Vertx) owner
The Vertx instance that created the context.
-
- (Array<String>) processArgs
The process args.
-
- (void) put(key, value)
Put some data in the context.
-
- (void) putLocal(key, value)
Put some local data in the context.
-
- (true, false) remove(key)
Remove some data from the context.
-
- (true, false) removeLocal(key)
Remove some local data from the context.
-
- (void) runOnContext(action) { ... }
Run the specified action asynchronously on the same context, some time after the current execution has completed.
-
- (true, false) isWorkerContext
Is the current context a worker context? NOTE! when running blocking code using Vertx#execute_blocking from a standard (not worker) verticle, the context will still an event loop context and this will return false.
Class Method Details
+ (Boolean) accept?(obj)
50 51 52 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 50 def @@j_api_type.accept?(obj) obj.class == Context end |
+ (Object) j_api_type
59 60 61 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 59 def self.j_api_type @@j_api_type end |
+ (Object) j_class
62 63 64 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 62 def self.j_class Java::IoVertxCore::Context.java_class end |
+ (true, false) isOnEventLoopThread
NOTE! This is not always the same as calling #is_event_loop_context. If you are running blocking code from an event loop context, then this will return false but #is_event_loop_context will return true.
288 289 290 291 292 293 294 295 296 297 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 288 def self.on_event_loop_thread? if !block_given? return Java::IoVertxCore::Context.java_method(:isOnEventLoopThread, []).call() end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling on_event_loop_thread?()" end end |
+ (true, false) isOnVertxThread
155 156 157 158 159 160 161 162 163 164 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 155 def self.on_vertx_thread? if !block_given? return Java::IoVertxCore::Context.java_method(:isOnVertxThread, []).call() end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling on_vertx_thread?()" end end |
+ (true, false) isOnWorkerThread
NOTE! This is not always the same as calling #is_worker_context. If you are running blocking code from an event loop context, then this will return true but #is_worker_context will return false.
125 126 127 128 129 130 131 132 133 134 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 125 def self.on_worker_thread? if !block_given? return Java::IoVertxCore::Context.java_method(:isOnWorkerThread, []).call() end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling on_worker_thread?()" end end |
+ (Object) unwrap(obj)
56 57 58 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 56 def @@j_api_type.unwrap(obj) obj.j_del end |
+ (Object) wrap(obj)
53 54 55 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 53 def @@j_api_type.wrap(obj) Context.new(obj) end |
Instance Method Details
- (Hash{String => Object}) config
370 371 372 373 374 375 376 377 378 379 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 370 def config if !block_given? return @j_del.java_method(:config, []).call() != nil ? JSON.parse(@j_del.java_method(:config, []).call().encode) : nil end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling config()" end end |
- (String) deploymentID
241 242 243 244 245 246 247 248 249 250 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 241 def deployment_id if !block_given? return @j_del.java_method(:deploymentID, []).call() end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling deployment_id()" end end |
- (true, false) isEventLoopContext
NOTE! when running blocking code using Vertx#execute_blocking from a standard (not worker) verticle, the context will still an event loop context and this will return true.
258 259 260 261 262 263 264 265 266 267 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 258 def event_loop_context? if !block_given? return @j_del.java_method(:isEventLoopContext, []).call() end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling event_loop_context?()" end end |
- (self) exceptionHandler(handler) { ... }
355 356 357 358 359 360 361 362 363 364 365 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 355 def exception_handler if true @j_del.java_method(:exceptionHandler, [Java::IoVertxCore::Handler.java_class]).call((Proc.new { |event| yield(::Vertx::Util::Utils.from_throwable(event)) unless !block_given? })) return self end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling exception_handler()" end end |
- (void) executeBlocking(blockingCodeHandler, resultHandler) { ... } - (void) executeBlocking(blockingCodeHandler, ordered, resultHandler) { ... }
This method returns an undefined value.
Safely execute some blocking code.
Executes the blocking code in the handler blockingCodeHandler
using a thread from the worker pool.
When the code is complete the handler resultHandler
will be called with the result on the original context
(e.g. on the original event loop of the caller).
A Future
instance is passed into blockingCodeHandler
. When the blocking code successfully completes,
the handler should call the Promise#complete or Promise#complete method, or the Promise#fail
method if it failed.
The blocking code should block for a reasonable amount of time (i.e no more than a few seconds). Long blocking operations or polling operations (i.e a thread that spin in a loop polling events in a blocking fashion) are precluded.
When the blocking operation lasts more than the 10 seconds, a message will be printed on the console by the blocked thread checker.
Long blocking operations should use a dedicated thread managed by the application, which can interact with verticles using the event-bus or #run_on_context
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 325 def execute_blocking(*args) if args[0].class == Proc && true && args[1] == nil if (block_given?) return @j_del.java_method(:executeBlocking, [Java::IoVertxCore::Handler.java_class,Java::IoVertxCore::Handler.java_class]).call((Proc.new { |event| args[0].call(::Vertx::Util::Utils.safe_create(event,::Vertx::Promise, nil)) unless args[0] == nil }),block_given? ? Proc.new { |ar| yield(ar.failed ? ar.cause : nil, ar.succeeded ? ::Vertx::Util::Utils.from_object(ar.result) : nil) } : promise) else promise = ::Vertx::Util::Utils.promise @j_del.java_method(:executeBlocking, [Java::IoVertxCore::Handler.java_class,Java::IoVertxCore::Handler.java_class]).call((Proc.new { |event| args[0].call(::Vertx::Util::Utils.safe_create(event,::Vertx::Promise, nil)) unless args[0] == nil }),block_given? ? Proc.new { |ar| yield(ar.failed ? ar.cause : nil, ar.succeeded ? ::Vertx::Util::Utils.from_object(ar.result) : nil) } : promise) return ::Vertx::Util::Utils.safe_create(promise.future(),::Vertx::Future, nil) end elsif args[0].class == Proc && (args[1].class == TrueClass || args[1].class == FalseClass) && true if (block_given?) return @j_del.java_method(:executeBlocking, [Java::IoVertxCore::Handler.java_class,Java::boolean.java_class,Java::IoVertxCore::Handler.java_class]).call((Proc.new { |event| args[0].call(::Vertx::Util::Utils.safe_create(event,::Vertx::Promise, nil)) unless args[0] == nil }),args[1],block_given? ? Proc.new { |ar| yield(ar.failed ? ar.cause : nil, ar.succeeded ? ::Vertx::Util::Utils.from_object(ar.result) : nil) } : promise) else promise = ::Vertx::Util::Utils.promise @j_del.java_method(:executeBlocking, [Java::IoVertxCore::Handler.java_class,Java::boolean.java_class,Java::IoVertxCore::Handler.java_class]).call((Proc.new { |event| args[0].call(::Vertx::Util::Utils.safe_create(event,::Vertx::Promise, nil)) unless args[0] == nil }),args[1],block_given? ? Proc.new { |ar| yield(ar.failed ? ar.cause : nil, ar.succeeded ? ::Vertx::Util::Utils.from_object(ar.result) : nil) } : promise) return ::Vertx::Util::Utils.safe_create(promise.future(),::Vertx::Future, nil) end end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling execute_blocking(#{args[0]},#{args[1]})" end end |
- (Object) get(key)
228 229 230 231 232 233 234 235 236 237 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 228 def get(*args) if args[0].class == String && !block_given? return ::Vertx::Util::Utils.from_object(@j_del.java_method(:get, [Java::java.lang.String.java_class]).call(args[0])) end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling get(#{args[0]})" end end |
- (Fixnum) getInstanceCount
92 93 94 95 96 97 98 99 100 101 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 92 def get_instance_count if !block_given? return @j_del.java_method(:getInstanceCount, []).call() end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling get_instance_count()" end end |
- (Object) getLocal(key)
214 215 216 217 218 219 220 221 222 223 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 214 def get_local(*args) if args[0].class == String && !block_given? return ::Vertx::Util::Utils.from_object(@j_del.java_method(:getLocal, [Java::java.lang.String.java_class]).call(args[0])) end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling get_local(#{args[0]})" end end |
- (::Vertx::Vertx) owner
67 68 69 70 71 72 73 74 75 76 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 67 def owner if !block_given? return ::Vertx::Util::Utils.safe_create(@j_del.java_method(:owner, []).call(),::Vertx::Vertx) end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling owner()" end end |
- (Array<String>) processArgs
80 81 82 83 84 85 86 87 88 89 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 80 def process_args if !block_given? return @j_del.java_method(:processArgs, []).call().to_a.map { |elt| elt } end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling process_args()" end end |
- (void) put(key, value)
This method returns an undefined value.
Put some data in the context.This can be used to share data between different handlers that share a context
186 187 188 189 190 191 192 193 194 195 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 186 def put(*args) if args[0].class == String && ::Vertx::Util::unknown_type.accept?(args[1]) && !block_given? return @j_del.java_method(:put, [Java::java.lang.String.java_class,Java::java.lang.Object.java_class]).call(args[0],::Vertx::Util::Utils.to_object(args[1])) end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling put(#{args[0]},#{args[1]})" end end |
- (void) putLocal(key, value)
This method returns an undefined value.
Put some local data in the context.This can be used to share data between different handlers that share a context
142 143 144 145 146 147 148 149 150 151 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 142 def put_local(*args) if args[0].class == String && ::Vertx::Util::unknown_type.accept?(args[1]) && !block_given? return @j_del.java_method(:putLocal, [Java::java.lang.String.java_class,Java::java.lang.Object.java_class]).call(args[0],::Vertx::Util::Utils.to_object(args[1])) end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling put_local(#{args[0]},#{args[1]})" end end |
- (true, false) remove(key)
169 170 171 172 173 174 175 176 177 178 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 169 def remove?(*args) if args[0].class == String && !block_given? return @j_del.java_method(:remove, [Java::java.lang.String.java_class]).call(args[0]) end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling remove?(#{args[0]})" end end |
- (true, false) removeLocal(key)
200 201 202 203 204 205 206 207 208 209 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 200 def remove_local?(*args) if args[0].class == String && !block_given? return @j_del.java_method(:removeLocal, [Java::java.lang.String.java_class]).call(args[0]) end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling remove_local?(#{args[0]})" end end |
- (void) runOnContext(action) { ... }
This method returns an undefined value.
Run the specified action asynchronously on the same context, some time after the current execution has completed.
272 273 274 275 276 277 278 279 280 281 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 272 def run_on_context if true return @j_del.java_method(:runOnContext, [Java::IoVertxCore::Handler.java_class]).call((Proc.new { |event| yield(nil) unless !block_given? })) end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling run_on_context()" end end |
- (true, false) isWorkerContext
NOTE! when running blocking code using Vertx#execute_blocking from a standard (not worker) verticle, the context will still an event loop context and this will return false.
109 110 111 112 113 114 115 116 117 118 |
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx/context.rb', line 109 def worker_context? if !block_given? return @j_del.java_method(:isWorkerContext, []).call() end if defined?(super) super else raise ArgumentError, "Invalid arguments when calling worker_context?()" end end |