Class: TorqueBox::Messaging::Message

Inherits:
Object
  • Object
show all
Defined in:
messaging/lib/torquebox/messaging/message.rb

Direct Known Subclasses

EdnMessage, JSONMessage, MarshalBase64Message, MarshalMessage, TextMessage

Constant Summary

DEFAULT_DECODE_ENCODING =
:marshal_base64
DEFAULT_ENCODE_ENCODING =
:marshal
ENCODING_PROPERTY =
"__ContentEncoding__"

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Message) initialize(jms_session, payload)

Returns a new instance of Message



35
36
37
38
39
40
# File 'messaging/lib/torquebox/messaging/message.rb', line 35

def initialize(jms_session, payload)
  @jms_message = self.class::JMS_TYPE == :text ? jms_session.create_text_message :
    jms_session.create_bytes_message
  set_encoding
  encode( payload )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(*args)



77
78
79
# File 'messaging/lib/torquebox/messaging/message.rb', line 77

def method_missing(*args)
  @jms_message.send(*args)
end

Instance Attribute Details

- (Object) :jms_message (readonly)

Returns the value of attribute :jms_message



24
25
26
# File 'messaging/lib/torquebox/messaging/message.rb', line 24

def :jms_message
  @:jms_message
end

Class Method Details

+ (Object) class_for_encoding(encoding)

Raises:

  • (ArgumentError)


124
125
126
127
128
# File 'messaging/lib/torquebox/messaging/message.rb', line 124

def class_for_encoding(encoding)
  klass = encoding_map[encoding.to_sym]
  raise ArgumentError.new( "No message class found for encoding '#{encoding}'" ) unless klass
  klass
end

+ (Object) encoding_map



116
117
118
# File 'messaging/lib/torquebox/messaging/message.rb', line 116

def encoding_map
  @encoding_map ||= { }
end

+ (Object) extract_encoding_from_message(jms_message)



130
131
132
# File 'messaging/lib/torquebox/messaging/message.rb', line 130

def extract_encoding_from_message(jms_message)
  jms_message.get_string_property( ENCODING_PROPERTY )
end

+ (Object) inherited(subclass)



96
97
98
99
100
# File 'messaging/lib/torquebox/messaging/message.rb', line 96

def inherited(subclass)
  class << subclass
    alias :new :__new__
  end
end

+ (Object) new(jms_message_or_session, payload = nil, encoding = nil)



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'messaging/lib/torquebox/messaging/message.rb', line 102

def new(jms_message_or_session, payload = nil, encoding = nil)
  if jms_message_or_session.is_a?( javax.jms::Session )
    encoding ||= ENV['DEFAULT_MESSAGE_ENCODING'] || DEFAULT_ENCODE_ENCODING
    klass = class_for_encoding( encoding.to_sym )
    klass.new( jms_message_or_session, payload )
  else
    encoding = extract_encoding_from_message( jms_message_or_session ) || DEFAULT_DECODE_ENCODING
    klass = class_for_encoding( encoding )
    msg = klass.allocate
    msg.initialize_from_message( jms_message_or_session )
    msg
  end
end

+ (Object) register_encoding(klass)



120
121
122
# File 'messaging/lib/torquebox/messaging/message.rb', line 120

def register_encoding(klass)
  encoding_map[klass::ENCODING] = klass
end

Instance Method Details

- (Object) decode



89
90
91
# File 'messaging/lib/torquebox/messaging/message.rb', line 89

def decode
  TorqueBox::Codecs.decode(@jms_message.text, self.class::ENCODING)
end

- (Object) encode(message)



85
86
87
# File 'messaging/lib/torquebox/messaging/message.rb', line 85

def encode(message)
  @jms_message.text = TorqueBox::Codecs.encode(message, self.class::ENCODING)
end

- (Object) initialize_from_message(jms_message)



42
43
44
# File 'messaging/lib/torquebox/messaging/message.rb', line 42

def initialize_from_message(jms_message)
  @jms_message = jms_message
end

- (Object) populate_message_headers(options)



50
51
52
53
54
55
56
57
58
59
# File 'messaging/lib/torquebox/messaging/message.rb', line 50

def populate_message_headers(options)
  return if options.nil?
  options.each do |key, value|
    case key.to_s
    when 'correlation_id' then @jms_message.setJMSCorrelationID(value)
    when 'reply_to'       then @jms_message.setJMSReplyTo(value)
    when 'type'           then @jms_message.setJMSType(value)
    end
  end
end

- (Object) populate_message_properties(properties)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'messaging/lib/torquebox/messaging/message.rb', line 61

def populate_message_properties(properties)
  return if properties.nil? or properties.empty?
  properties.each do |key, value|
    case value
      when Integer
        @jms_message.set_long_property(key.to_s, value)
      when Float
        @jms_message.set_double_property(key.to_s, value)
      when TrueClass, FalseClass
        @jms_message.set_boolean_property(key.to_s, value)
      else
        @jms_message.set_string_property(key.to_s, value.to_s)
    end
  end
end

- (Boolean) respond_to?(symbol, include_private = false)

Returns:

  • (Boolean)


81
82
83
# File 'messaging/lib/torquebox/messaging/message.rb', line 81

def respond_to?(symbol, include_private = false)
  super || @jms_message.respond_to?(symbol, include_private)
end

- (Object) set_encoding



46
47
48
# File 'messaging/lib/torquebox/messaging/message.rb', line 46

def set_encoding
  @jms_message.set_string_property( ENCODING_PROPERTY, self.class::ENCODING.to_s )
end