rubywmq 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : put() : Put a single message to a queue
19
+ # Open the queue so that multiple puts can be performed
20
+ #
21
+ require 'wmq/wmq'
22
+
23
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
24
+ qmgr.open_queue(:q_name=>'TEST.QUEUE', :mode=>:output) do |queue|
25
+
26
+ # First message
27
+ queue.put(:data => 'Hello World')
28
+
29
+ # Second message
30
+ # This message will have new message and correlation id's
31
+ queue.put(:data => 'Hello Again')
32
+ end
33
+ end
34
+
@@ -0,0 +1,42 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : put() : Put two Messages to a queue
19
+ # Open the queue so that multiple puts can be performed
20
+ # Ensure that all messages have the same correlation id
21
+ #
22
+ require 'wmq/wmq'
23
+
24
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
25
+ qmgr.open_queue(:q_name=>'TEST.QUEUE', :mode=>:output) do |queue|
26
+
27
+ message = WMQ::Message.new
28
+
29
+ # First message
30
+ # Results in a WMQ generated msg_id and empty correl_id
31
+ message.data = 'Hello World'
32
+ queue.put(:message => message)
33
+
34
+ # Second message
35
+ # new_msg_id will cause the second message to have a new message id
36
+ # otherwise both messages will have the same message and correlation id's
37
+ # This message will have the same correlation id as the first message (empty)
38
+ message.data = 'Hello Again'
39
+ p message.descriptor
40
+ queue.put(:message => message, :new_msg_id => true)
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : put() : Put a message to a queue with a dead letter header
19
+ # Open the queue so that multiple puts can be performed
20
+ #
21
+ require 'wmq/wmq'
22
+
23
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
24
+ qmgr.open_queue(:q_name=>'TEST.QUEUE', :mode=>:output) do |queue|
25
+ message = WMQ::Message.new
26
+ message.data = 'Hello World'
27
+
28
+ message.headers = [
29
+ {:header_type =>:dead_letter_header,
30
+ :reason => WMQ::MQRC_UNKNOWN_REMOTE_Q_MGR,
31
+ :dest_q_name =>'ORIGINAL_QUEUE_NAME',
32
+ :dest_q_mgr_name =>'BAD_Q_MGR'}
33
+ ]
34
+
35
+ message.descriptor[:format] = WMQ::MQFMT_STRING
36
+
37
+ queue.put(:message=>message)
38
+ end
39
+ end
40
+
@@ -0,0 +1,37 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : put() : Put a single message to a queue
19
+ # Create the queue if it does not already exist
20
+ #
21
+ # Note : The queue name is that of the model queue
22
+ # The dynamic_q_name is the actual queue name
23
+ #
24
+ # This sample is more usefull if the model queue was a Permanent Dynamic one.
25
+ # That way the queue would remain after termination of this code.
26
+ # In this sample the queue will disappear when this program terminates
27
+ #
28
+ require 'wmq/wmq'
29
+
30
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
31
+ qmgr.open_queue(:q_name => 'SYSTEM.DEFAULT.MODEL.QUEUE',
32
+ :dynamic_q_name => 'TEST.QUEUE.SAMPLE',
33
+ :mode => :output
34
+ ) do |queue|
35
+ queue.put(:data => 'Hello World')
36
+ end
37
+ end
@@ -0,0 +1,66 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : put() : Put a message to a queue with a Refernce header
19
+ # Open the queue so that multiple puts can be performed
20
+ #
21
+ require 'wmq/wmq'
22
+
23
+ # The Rules Format header (MQRFH) allows a list of name value pairs to be sent along
24
+ # with a WMQ message. These name value pairs are represented as follows on the "wire":
25
+ # Name1 Value1 Name2 Value2 Name3 Value3
26
+ #
27
+ # Ruby WMQ converts the above string of data into a Ruby hash by
28
+ # using the name as the key, as follows:
29
+ # data = {
30
+ # 'Name1' => 'Value1',
31
+ # 'Name2' => 'Value2',
32
+ # 'Name3' => 'Value3'
33
+ # }
34
+ #
35
+ # Since a name can consist of any character except null, it is stored as a String
36
+ #
37
+ # Note: It is possible to send or receive the same Name with multiple values using an array.
38
+ # E.g. Name1 Value1 Name2 Value2 Name1 Value3
39
+ # Becomes:
40
+ # data = {
41
+ # 'Name1' => ['Value1', 'Value3'],
42
+ # 'Name2' => 'Value2'
43
+ # }
44
+ #
45
+ # Note: Since a Hash does not preserve order, reading a Rules Format Header and then writing
46
+ # it out immediately again could result in re-ordering of the name value pairs.
47
+ #
48
+
49
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
50
+ qmgr.open_queue(:q_name=>'TEST.QUEUE', :mode=>:output) do |queue|
51
+ message = WMQ::Message.new
52
+ message.data = 'Hello World'
53
+
54
+ message.headers = [
55
+ {:header_type =>:rf_header,
56
+ :name_value => {'name1' => 'value1',
57
+ 'name2' => 'value2',
58
+ 'name3' => ['value 3a', 'value 3b']}
59
+ }]
60
+
61
+ message.descriptor[:format] = WMQ::MQFMT_STRING
62
+
63
+ queue.put(:message=>message)
64
+ end
65
+ end
66
+
@@ -0,0 +1,42 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : put() : Put a message to a queue with a Refernce header
19
+ # Open the queue so that multiple puts can be performed
20
+ #
21
+ require 'wmq/wmq'
22
+
23
+ # The Rules Format header2 (MQRFH2) allows a an XML-like string to be passed as a header
24
+ # to the data.
25
+ #
26
+
27
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
28
+ qmgr.open_queue(:q_name=>'TEST.QUEUE', :mode=>:output) do |queue|
29
+ message = WMQ::Message.new
30
+ message.data = 'Hello World'
31
+
32
+ message.headers = [
33
+ {:header_type =>:rf_header_2,
34
+ :xml => '<hello>to the world</hello>'
35
+ }]
36
+
37
+ message.descriptor[:format] = WMQ::MQFMT_STRING
38
+
39
+ queue.put(:message=>message)
40
+ end
41
+ end
42
+
@@ -0,0 +1,42 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : put() : Put a message to a queue with a Refernce header
19
+ # Open the queue so that multiple puts can be performed
20
+ #
21
+ require 'wmq/wmq'
22
+
23
+ # The Rules Format header2 (MQRFH2) allows a an XML-like string to be passed as a header
24
+ # to the data.
25
+ #
26
+
27
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
28
+ qmgr.open_queue(:q_name=>'TEST.QUEUE', :mode=>:output) do |queue|
29
+ message = WMQ::Message.new
30
+ message.data = 'Hello World'
31
+
32
+ message.headers = [
33
+ {:header_type =>:rf_header_2,
34
+ :xml => ['<hello>to the world</hello>', '<another>xml like string</another>'],
35
+ }]
36
+
37
+ message.descriptor[:format] = WMQ::MQFMT_STRING
38
+
39
+ queue.put(:message=>message)
40
+ end
41
+ end
42
+
@@ -0,0 +1,32 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : put() : Put a message to a queue with a Transmission header
19
+ #
20
+ require 'wmq/wmq'
21
+
22
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
23
+ qmgr.open_queue(:q_name=>'TEST.QUEUE', :mode=>:output) do |queue|
24
+ message = WMQ::Message.new
25
+ message.data = "Test message from 'LOCALQMS1'"
26
+ message.descriptor = {:original_length=>-1, :priority=>0, :put_time=>"18510170", :msg_id=>"AMQ LOCALQMS1 E\233\001\237 \000\003\005", :expiry=>-1, :persistence=>0, :reply_to_q=>"MQMON", :correl_id=>"AMQ LOCALQMS1 E\233\001\237 \000\003\004", :feedback=>0, :offset=>0, :report=>0, :msg_flags=>0, :reply_to_q_mgr=>"LOCALQMS1", :appl_identity_data=>"", :put_appl_name=>"LOCALQMS1", :user_identifier=>"mqm", :msg_seq_number=>1, :appl_origin_data=>"", :accounting_token=>"\026\001\005\025\000\000\000\271U\305\002\261\022\362\321\021D\3206\357\003\000\000\000\000\000\000\000\000\000\000\v", :backout_count=>0, :coded_char_set_id=>437, :put_appl_type=>7, :msg_type=>8, :group_id=>"", :put_date=>"20070109", :format=>"MQSTR", :encoding=>546}
27
+ message.headers = [{:priority=>0, :remote_q_mgr_name=>"OTHER.QMGR", :put_time=>"18510170", :msg_id=>"AMQ LOCALQMS1 E\233\001\237 \000\003\004", :expiry=>-1, :persistence=>0, :remote_q_name=>"OTHER.Q", :header_type=>:xmit_q_header, :reply_to_q=>"MQMON", :correl_id=>"", :feedback=>0, :report=>0, :reply_to_q_mgr=>"LOCALQMS1", :appl_identity_data=>"", :put_appl_name=>"uments\\MQ\\MQMon\\mqmonntp.exe", :user_identifier=>"mqm", :appl_origin_data=>"", :accounting_token=>"\026\001\005\025\000\000\000\271U\305\002\261\022\362\321\021D\3206\357\003\000\000\000\000\000\000\000\000\000\000\v", :backout_count=>0, :coded_char_set_id=>437, :put_appl_type=>11, :msg_type=>8, :put_date=>"20070109", :encoding=>546}]
28
+
29
+ queue.put(:message=>message)
30
+ end
31
+ end
32
+
@@ -0,0 +1,59 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : request.rb
19
+ #
20
+ # Sample program that demonstrates how to send a request message
21
+ # and then block until the response is received from the server.
22
+ #
23
+ # A temporary Dynamic Reply To Queue is used with non-persistent messaging
24
+ #
25
+ require 'wmq'
26
+
27
+ wait_seconds = 30
28
+
29
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
30
+ qmgr.open_queue(:q_name => 'SYSTEM.DEFAULT.MODEL.QUEUE',
31
+ :dynamic_q_name=> 'REQUEST.*',
32
+ :mode => :input
33
+ ) do |reply_queue|
34
+
35
+ message = WMQ::Message.new()
36
+ message.descriptor = { :msg_type => WMQ::MQMT_REQUEST,
37
+ :reply_to_q => reply_queue.name,
38
+ :reply_to_q_mgr=> qmgr.name,
39
+ :format => WMQ::MQFMT_STRING,
40
+ :expiry => wait_seconds*10} # Measured in tenths of a second
41
+ message.data = 'Hello World'
42
+
43
+ # Send request message
44
+ qmgr.put(:q_name=>'TEST.QUEUE', :message=>message)
45
+
46
+ # Copy outgoing Message id to correlation id
47
+ message.descriptor[:correl_id]=message.descriptor[:msg_id]
48
+
49
+ # Wait for reply
50
+ # Only get the message that matches the correlation id set above
51
+ if reply_queue.get(:wait=>wait_seconds*1000, :message=>message, :match=>WMQ::MQMO_MATCH_CORREL_ID)
52
+ puts "Received:"
53
+ puts message.data
54
+ else
55
+ # get returns false when no message received. Also: message.data = nil
56
+ puts "Timed Out waiting for a reply from the server"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,96 @@
1
+ ################################################################################
2
+ # Copyright 2006 J. Reid Morrison. Dimension Solutions, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #
18
+ # Sample : Sample program to show how to write a server side application
19
+ #
20
+ # This server times out after 60 seconds, but could be modified to
21
+ # run forever. Example:
22
+ # queue.each(:wait=>-1) do |message|
23
+ #
24
+ # Note: - All calls are being performed under synchpoint control to
25
+ # prevent messages being if the program terminates unexpectedly
26
+ # or an error occurrs.
27
+ # Uses: :sync=>true
28
+ # - Queue#each will backout any message changes if an excecption is raised
29
+ # but not handled within the each block
30
+ #
31
+ # A "well-behaved" WebSphere MQ application should adhere to the following rules:
32
+ # - Perform puts and gets under synchpoint where applicable
33
+ # - Only send replies to Request messages. No reply for Datagrams
34
+ # - Set the message type to Reply when replying to a request message
35
+ # - Reply with:
36
+ # - Remaining Expiry (Ideally deduct any processing time since get)
37
+ # - Same priority as received message
38
+ # - Same persistence as received message
39
+ # - Adhere to the Report options supplied for message and correlation id's
40
+ # in reply message
41
+ # - All headers must be returned on reply messages
42
+ # - This allows the calling application to store state information
43
+ # in these headers
44
+ # - Unless of course if the relevant header is input only and used
45
+ # for completing the request
46
+ # - In this case any remaining headers should be returned
47
+ # to the caller
48
+ # - If an error occurs trying to process the message, an error message
49
+ # must be sent back to the requesting application
50
+ # - If the reply fails, it must be put to the dead letter queue
51
+ # with the relevant dead letter header and reason
52
+ #
53
+ # Note: - It is not recommended to run server side MQ applications over a client
54
+ # connection.
55
+ # - Client connections require substantially more error handling.
56
+ # - E.g. Connection to queue manager can be lost due to netowk issues
57
+ # - Need to go into some sort of retry state attempting
58
+ # to reconnect to the queue manager
59
+ # - What about any work that was in progress?
60
+ # - Need to re-open any queues
61
+ # - Do any changes to other resources need to be undone first?
62
+ # - E.g. Database, File etc..
63
+ # - etc....
64
+ #
65
+ require 'wmq/wmq'
66
+
67
+ WMQ::QueueManager.connect(:q_mgr_name=>'REID') do |qmgr|
68
+ qmgr.open_queue(:q_name=>'TEST.QUEUE', :mode=>:input) do |queue|
69
+ queue.each(:wait=>60000, :sync=>true, :convert=>true) do |request|
70
+ puts "Data Received:"
71
+ puts request.data
72
+
73
+ begin
74
+ reply = WMQ::Message.new
75
+ reply.data = 'Echo back:'+request.data
76
+
77
+ qmgr.put_to_reply_q(:message=>reply,
78
+ :request_message=>request, # Only replies if message type is request
79
+ :sync=>true)
80
+
81
+ rescue WMQ::WMQException => exc
82
+ # Failed to send reply, put message to the Dead Letter Queue and add a dead letter header
83
+ p exc
84
+ puts "Failed to reply to sender, Put to dead letter queue"
85
+ qmgr.put_to_dead_letter_q(:message=>request,
86
+ :reason=>WMQ::MQRC_UNKNOWN_REMOTE_Q_MGR,
87
+ :q_name=>queue.name,
88
+ :sync=>true)
89
+ # If it fails to put to the dead letter queue, this program will terminate and
90
+ # the changes will be "backed out". E.g. Queue Full, ...
91
+ end
92
+ qmgr.commit
93
+ end
94
+ end
95
+ puts 'No more messages found after 60 second wait interval'
96
+ end