[r362]: branches / 0.71 / lib / SOAP / Transport / MQ.pm  Maximize  Restore  History

Download this file

261 lines (194 with data), 6.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# ======================================================================
#
# Copyright (C) 2000-2001 Paul Kulchenko (paulclinger@yahoo.com)
# SOAP::Lite is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
#
# $Id: MQ.pm 353 2010-03-17 21:08:34Z kutterma $
#
# ======================================================================
package SOAP::Transport::MQ;
use strict;
our $VERSION = 0.711;
use MQClient::MQSeries;
use MQSeries::QueueManager;
use MQSeries::Queue;
use MQSeries::Message;
use URI;
use URI::Escape;
use SOAP::Lite;
sub requestqueue {
my $self = shift;
$self = $self->new() if not ref $self;
if (@_) {
$self->{ _requestqueue } = shift;
return $self;
}
return $self->{ _requestqueue };
}
sub replyqueue {
my $self = shift;
$self = $self->new() if not ref $self;
if (@_) {
$self->{ _replyqueue } = shift;
return $self;
}
return $self->{ _replyqueue };
}
# ======================================================================
package URI::mq; # ok, lets do 'mq://' scheme
require URI::_server;
require URI::_userpass;
@URI::mq::ISA=qw(URI::_server URI::_userpass);
# mq://user@host:port?Channel=A;QueueManager=B;RequestQueue=C;ReplyQueue=D
# ^^ ^^^^ ^^^^ ^^^^ ^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^
# ======================================================================
package SOAP::Transport::MQ::Client;
use vars qw(@ISA);
@ISA = qw(SOAP::Client SOAP::Transport::MQ);
use MQSeries qw(:constants);
sub DESTROY {
SOAP::Trace::objects('()');
}
sub new {
my $class = shift;
return $class if ref $class;
my(@params, @methods);
while (@_) {
$class->can($_[0])
? push(@methods, shift() => shift)
: push(@params, shift)
}
my $self = bless {@params} => $class;
while (@methods) {
my($method, $params) = splice(@methods,0,2);
$self->$method(ref $params eq 'ARRAY' ? @$params : $params)
}
SOAP::Trace::objects('()');
return $self;
}
sub endpoint {
my $self = shift;
return $self->SUPER::endpoint unless @_;
my $endpoint = shift;
# nothing to do if new endpoint is the same as the current one
{
no warnings qw(uninitialized);
return $self if $self->SUPER::endpoint eq $endpoint;
}
my $uri = URI->new($endpoint);
my %parameters = (%$self, map {URI::Escape::uri_unescape($_)} map {split/=/,$_,2} split /[&;]/, $uri->query || '');
$ENV{MQSERVER} = sprintf "%s/TCP/%s(%s)", $parameters{Channel}, $uri->host, $uri->port
if $uri->host;
my $qmgr = MQSeries::QueueManager->new(
QueueManager => $parameters{QueueManager}
)
|| die "Unable to connect to queue manager $parameters{QueueManager}\n";
$self->requestqueue(
MQSeries::Queue->new (
QueueManager => $qmgr,
Queue => $parameters{RequestQueue},
Mode => 'output',
)
|| die "Unable to open $parameters{RequestQueue}\n"
);
$self->replyqueue(
MQSeries::Queue->new (
QueueManager => $qmgr,
Queue => $parameters{ReplyQueue},
Mode => 'input',
)
|| die "Unable to open $parameters{ReplyQueue}\n"
);
return $self->SUPER::endpoint($endpoint);
}
sub send_receive {
my($self, %parameters) = @_;
my($envelope, $endpoint) = @parameters{qw(envelope endpoint)};
$self->endpoint($endpoint ||= $self->endpoint);
%parameters = (%$self, %parameters);
my $expiry = $parameters{Expiry} || 60000;
SOAP::Trace::debug($envelope);
my $request = MQSeries::Message->new (
MsgDesc => {
Format => MQFMT_STRING,
Expiry => $expiry
},
Data => $envelope,
);
$self->requestqueue->Put(Message => $request)
|| die "Unable to put message to queue\n";
my $reply = MQSeries::Message->new (
MsgDesc => {CorrelId => $request->MsgDesc('MsgId')},
);
my $result = $self->replyqueue->Get (
Message => $reply,
Wait => $expiry,
);
my $msg = $reply->Data if $result > 0;
SOAP::Trace::debug($msg);
my $code = $result > 0
? undef
: $result < 0
? 'Timeout'
: 'Error occured while waiting for response';
$self->code($code);
$self->message($code);
$self->is_success(!defined $code || $code eq '');
$self->status($code);
return $msg;
}
# ======================================================================
package SOAP::Transport::MQ::Server;
use Carp ();
use vars qw(@ISA $AUTOLOAD);
@ISA = qw(SOAP::Server SOAP::Transport::MQ);
use MQSeries qw(:constants);
sub new {
my $class = shift;
return $class if ref $class;
die "missing parameter (uri)" if not @_;
my $uri = URI->new(shift);
my $self = $class->SUPER::new(@_);
my %parameters = (%$self, map {URI::Escape::uri_unescape($_)} map {split/=/,$_,2} split /[&;]/, $uri->query || '');
$ENV{MQSERVER} = sprintf "%s/TCP/%s(%s)", $parameters{Channel}, $uri->host, $uri->port
if $uri->host;
my $qmgr = MQSeries::QueueManager->new(QueueManager => $parameters{QueueManager}) ||
Carp::croak "Unable to connect to queue manager $parameters{QueueManager}";
$self->requestqueue(MQSeries::Queue->new (
QueueManager => $qmgr,
Queue => $parameters{RequestQueue},
Mode => 'input',
) || Carp::croak "Unable to open $parameters{RequestQueue}");
$self->replyqueue(MQSeries::Queue->new (
QueueManager => $qmgr,
Queue => $parameters{ReplyQueue},
Mode => 'output',
) || Carp::croak "Unable to open $parameters{ReplyQueue}");
return $self;
}
sub handle {
my $self = shift->new;
my $msg = 0;
while (1) {
my $request = MQSeries::Message->new;
# nonblock waiting
$self->requestqueue->Get( Message => $request,)
|| die "Error occured while waiting for requests\n";
return $msg if $self->requestqueue->Reason == MQRC_NO_MSG_AVAILABLE;
my $reply = MQSeries::Message->new (
MsgDesc => {
CorrelId => $request->MsgDesc('MsgId'),
Expiry => $request->MsgDesc('Expiry'),
},
Data => $self->SUPER::handle($request->Data),
);
$self->replyqueue->Put (
Message => $reply,
) || die "Unable to put reply message\n";
$msg++;
}
}
# ======================================================================
1;
__END__