Class: TorqueBox::Messaging::Message

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

Constant Summary collapse

DEFAULT_DECODE_ENCODING =

if no encoding specified in the message itself assume the legacy encoding

:marshal_base64
DEFAULT_ENCODE_ENCODING =

if no encoding specified when creating a message and no global defaut set use :marshal

:marshal
ENCODING_PROPERTY =
"__ContentEncoding__"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jms_session, payload) ⇒ Message

Returns a new instance of Message.



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

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

#method_missing(*args) ⇒ Object



75
76
77
# File 'lib/torquebox/messaging/message.rb', line 75

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

Instance Attribute Details

#jms_messageObject (readonly)

Returns the value of attribute jms_message.



22
23
24
# File 'lib/torquebox/messaging/message.rb', line 22

def jms_message
  @jms_message
end

Class Method Details

.__new__Object



84
# File 'lib/torquebox/messaging/message.rb', line 84

alias :__new__ :new

.class_for_encoding(encoding) ⇒ Object

Raises:

  • (ArgumentError)


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

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

.encoding_mapObject



106
107
108
# File 'lib/torquebox/messaging/message.rb', line 106

def encoding_map
  @encoding_map ||= { }
end

.extract_encoding_from_message(jms_message) ⇒ Object



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

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

.inherited(subclass) ⇒ Object



86
87
88
89
90
# File 'lib/torquebox/messaging/message.rb', line 86

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

.new(jms_message_or_session, payload = nil, encoding = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/torquebox/messaging/message.rb', line 92

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

.register_encoding(klass) ⇒ Object



110
111
112
# File 'lib/torquebox/messaging/message.rb', line 110

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

Instance Method Details

#initialize_from_message(jms_message) ⇒ Object



40
41
42
# File 'lib/torquebox/messaging/message.rb', line 40

def initialize_from_message(jms_message)
  @jms_message = jms_message
end

#populate_message_headers(options) ⇒ Object



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

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

#populate_message_properties(properties) ⇒ Object



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

def populate_message_properties(properties)
  return if properties.nil?
  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

#respond_to?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/torquebox/messaging/message.rb', line 79

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

#set_encodingObject



44
45
46
# File 'lib/torquebox/messaging/message.rb', line 44

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