Class: Vertx::Context

Inherits:
Object
  • Object
show all
Defined in:
/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb

Overview

The execution context of a Handler execution.

When Vert.x provides an event to a handler or calls the start or stop methods of a Verticle, 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.

Instance Method Summary (collapse)

Instance Method Details

- (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.

Returns:

  • (Hash{String => Object})
    the configuration of the deployment or null if not a Verticle deployment

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 65

def config
  if !block_given?
    return @j_del.java_method(:config, []).call() != nil ? JSON.parse(@j_del.java_method(:config, []).call().encode) : nil
  end
  raise ArgumentError, "Invalid arguments when calling config()"
end

- (String) deployment_id

If the context is associated with a Verticle deployment, this returns the deployment ID of that deployment.

Returns:

  • (String)
    the deployment ID of the deployment or null if not a Verticle deployment

Raises:

  • (ArgumentError)


56
57
58
59
60
61
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 56

def deployment_id
  if !block_given?
    return @j_del.java_method(:deploymentID, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling deployment_id()"
end

- (true, false) event_loop_context?

@return true if this is an event loop context, false otherwise

Returns:

  • (true, false)

Raises:

  • (ArgumentError)


81
82
83
84
85
86
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 81

def event_loop_context?
  if !block_given?
    return @j_del.java_method(:isEventLoopContext, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling event_loop_context?()"
end

- (Object) get(key = nil)

Get some data from the context.

Parameters:

  • key (String) (defaults to: nil)
    the key of the data

Returns:

  • (Object)
    the data

Raises:

  • (ArgumentError)


106
107
108
109
110
111
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 106

def get(key=nil)
  if key.class == String && !block_given?
    return ::Vertx::Util::Utils.from_object(@j_del.java_method(:get, [Java::java.lang.String.java_class]).call(key))
  end
  raise ArgumentError, "Invalid arguments when calling get(key)"
end

- (true, false) multi_threaded?

@return true if this is a multi-threaded worker context, false otherwise

Returns:

  • (true, false)

Raises:

  • (ArgumentError)


97
98
99
100
101
102
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 97

def multi_threaded?
  if !block_given?
    return @j_del.java_method(:isMultiThreaded, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling multi_threaded?()"
end

- (::Vertx::Vertx) owner

@return The Vertx instance that created the context

Returns:

Raises:

  • (ArgumentError)


135
136
137
138
139
140
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 135

def owner
  if !block_given?
    return ::Vertx::Vertx.new(@j_del.java_method(:owner, []).call())
  end
  raise ArgumentError, "Invalid arguments when calling owner()"
end

- (Array<String>) process_args

The process args

Returns:

  • (Array<String>)

Raises:

  • (ArgumentError)


73
74
75
76
77
78
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 73

def process_args
  if !block_given?
    return @j_del.java_method(:processArgs, []).call().to_a.map { |elt| elt }
  end
  raise ArgumentError, "Invalid arguments when calling process_args()"
end

- (void) put(key = nil, value = nil)

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

Parameters:

  • key (String) (defaults to: nil)
    the key of the data
  • value (Object) (defaults to: nil)
    the data

Raises:

  • (ArgumentError)


118
119
120
121
122
123
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 118

def put(key=nil,value=nil)
  if key.class == String && (value.class == String  || value.class == Hash || value.class == Array || value.class == NilClass || value.class == TrueClass || value.class == FalseClass || value.class == Fixnum || value.class == Float) && !block_given?
    return @j_del.java_method(:put, [Java::java.lang.String.java_class,Java::java.lang.Object.java_class]).call(key,::Vertx::Util::Utils.to_object(value))
  end
  raise ArgumentError, "Invalid arguments when calling put(key,value)"
end

- (true, false) remove?(key = nil)

Remove some data from the context.

Parameters:

  • key (String) (defaults to: nil)
    the key to remove

Returns:

  • (true, false)
    true if removed successfully, false otherwise

Raises:

  • (ArgumentError)


127
128
129
130
131
132
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 127

def remove?(key=nil)
  if key.class == String && !block_given?
    return @j_del.java_method(:remove, [Java::java.lang.String.java_class]).call(key)
  end
  raise ArgumentError, "Invalid arguments when calling remove?(key)"
end

- (void) run_on_context { ... }

This method returns an undefined value.

Run the specified action asynchronously on the same context, some time after the current execution has completed.

Yields:

  • the action to run

Raises:

  • (ArgumentError)


48
49
50
51
52
53
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 48

def run_on_context
  if block_given?
    return @j_del.java_method(:runOnContext, [Java::IoVertxCore::Handler.java_class]).call(Proc.new { yield })
  end
  raise ArgumentError, "Invalid arguments when calling run_on_context()"
end

- (true, false) worker?

@return true if this is an worker context, false otherwise

Returns:

  • (true, false)

Raises:

  • (ArgumentError)


89
90
91
92
93
94
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/src/main/resources/vertx/context.rb', line 89

def worker?
  if !block_given?
    return @j_del.java_method(:isWorker, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling worker?()"
end