torquebox-stomp 2.3.2 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+
2
+ # Copyright 2008-2013 Red Hat, Inc, and individual contributors.
3
+ #
4
+ # This is free software; you can redistribute it and/or modify it
5
+ # under the terms of the GNU Lesser General Public License as
6
+ # published by the Free Software Foundation; either version 2.1 of
7
+ # the License, or (at your option) any later version.
8
+ #
9
+ # This software is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this software; if not, write to the Free
16
+ # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17
+ # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18
+
19
+ module org.projectodd.stilts.stomplet::Subscriber
20
+
21
+ def [](name)
22
+ self.getParameter( name.to_s )
23
+ end
24
+
25
+ end
@@ -19,41 +19,33 @@ module TorqueBox
19
19
  module Stomp
20
20
  class JmsStomplet
21
21
 
22
- include TorqueBox::Injectors
23
-
24
22
  def initialize()
25
- @connection_factory = fetch( 'xa-connection-factory' )
23
+ @connection_factory = TorqueBox.fetch( 'xa-connection-factory' )
26
24
  @subscriptions = {}
27
25
  end
28
-
29
- def xa_resources
30
- @xa_resources
31
- end
32
-
26
+
27
+ #def xa_resources
28
+ # @xa_resources
29
+ #end
30
+
33
31
  def configure(stomplet_config)
34
- @connection = @connection_factory.create_connection
32
+ @connection = @connection_factory.create_xa_connection
35
33
  @connection.start
36
- @session = @connection.create_session
34
+ @session = @connection.create_session( false )
37
35
  @xa_resources = [ @session.xa_resource ]
38
36
  end
39
-
37
+
40
38
  def destroy
41
39
  @session.close
42
40
  @connection.close
43
41
  end
44
42
 
45
- # -----
46
- # -----
47
-
48
43
  def on_unsubscribe(subscriber)
49
44
  subscriptions = @subscriptions.delete( subscriber )
50
45
  (subscriptions || []).each do |subscription|
51
46
  subscription.close
52
47
  end
53
48
  end
54
-
55
- # -----
56
- # -----
57
49
 
58
50
  def queue(name)
59
51
  jms_session = @session.jms_session
@@ -69,30 +61,30 @@ module TorqueBox
69
61
  return queue(name) if ( type.to_sym == :queue )
70
62
  topic(name)
71
63
  end
72
-
73
- def subscribe_to(subscriber, destination, selector=nil)
64
+
65
+ def subscribe_to(subscriber, destination, selector=nil, options={})
74
66
  jms_session = @session.jms_session
75
67
  java_destination = @session.java_destination( destination )
76
68
  consumer = @session.jms_session.create_consumer( java_destination.to_java, selector )
77
- consumer.message_listener = MessageListener.new( subscriber )
69
+ consumer.message_listener = MessageListener.new( subscriber, options )
78
70
  @subscriptions[ subscriber ] ||= []
79
71
  @subscriptions[ subscriber ] << consumer
80
72
  end
81
-
82
- def send_to(destination, stomp_message, headers={})
73
+
74
+ def send_to(destination, stomp_message, headers={}, options={})
83
75
  jms_session = @session.jms_session
84
76
  java_destination = @session.java_destination( destination )
85
-
77
+
86
78
  producer = @session.jms_session.create_producer( java_destination.to_java )
87
79
 
88
- case ( stomp_message )
80
+ case ( stomp_message )
89
81
  when org.projectodd.stilts.stomp::StompMessage
90
82
  content = stomp_message.content_as_string
91
83
  else
92
- content = stomp_message.to_s
84
+ content = stomp_message
93
85
  end
94
86
 
95
- encoded_message = TorqueBox::Messaging::Message.new( @session.jms_session, content )
87
+ encoded_message = TorqueBox::Messaging::Message.new( @session.jms_session, content, options[:encoding] )
96
88
  jms_message = encoded_message.jms_message
97
89
 
98
90
  if ( stomp_message.is_a?( org.projectodd.stilts.stomp::StompMessage ) )
@@ -110,26 +102,37 @@ module TorqueBox
110
102
 
111
103
  producer.send( jms_message )
112
104
  end
113
-
105
+
106
+ # @api private
114
107
  class MessageListener
115
108
  include javax.jms.MessageListener
116
-
117
- def initialize(subscriber)
109
+
110
+ def initialize(subscriber, options={})
118
111
  @subscriber = subscriber
112
+ @encoding = options[:encoding]
119
113
  end
120
-
114
+
121
115
  def onMessage(jms_message)
122
- stomp_message = TorqueBox::Stomp::Message.new( TorqueBox::Messaging::Message.new( jms_message ).decode )
116
+ content = TorqueBox::Messaging::Message.new( jms_message ).decode
117
+ ( content = TorqueBox::Codecs.encode( content, @encoding ) ) if @encoding
118
+ stomp_message = TorqueBox::Stomp::Message.new( content )
123
119
  jms_message.property_names.each do |name|
124
120
  value = jms_message.getObjectProperty( name ).to_s
125
121
  stomp_message.headers.put( name.to_s.to_java( java.lang.String ), value.to_java( java.lang.String) ) if value
126
122
  end
123
+ if ( stomp_message.headers[ 'content-type' ] == nil )
124
+ if ( @encoding == :json )
125
+ stomp_message.headers.put( "content-type", "application/json" )
126
+ else
127
+ stomp_message.headers.put( "content-type", "text/plain" )
128
+ end
129
+ end
127
130
  @subscriber.send( stomp_message )
128
131
  end
129
-
132
+
130
133
  end
131
-
134
+
132
135
  end
133
136
 
134
137
  end
135
- end
138
+ end