Loan Broker example
You can also read this article by Mohan Vamsi which describes this example:
Implement two-way communication among ESB components
Info |
---|
You can use a PHP Stomp client to test this example... - Patch examples/loan-broker/src/components/loanbroker/LoanBroker.java with the patch below to allow String parameters
- Configure activemq.xml to have a <transportConnector uri="stomp://localhost:61626"/>
- Run: ant setup as per the README.txt
- Run: ../../bin/servicemix servicemix.xml (as per the README.txt)
- Download Dejan Bosanac's extended Stomp PHP library from: http://www.nighttale.net/download/stomp.zip
- Copy Stomp.php and JSON.php to an appropriate location
- Put the following PHP code into LoanBrokerTest.php
- Run: php LoanBrokerTest.php (sample response message output shown below)
Code Block |
---|
title | LoanBroker.java.diff |
---|
|
148,149c148,161
< ag.amount = (Double) getInProperty(exchange, Constants.PROPERTY_AMOUNT);
< ag.duration = (Integer) getInProperty(exchange, Constants.PROPERTY_DURATION);
---
>
> Object amountObj = getInProperty(exchange, Constants.PROPERTY_AMOUNT);
> if(amountObj instanceof String) {
> ag.amount = new Double((String)amountObj);
> } else {
> ag.amount = (Double) amountObj;
> }
>
> Object durationObj = getInProperty(exchange, Constants.PROPERTY_DURATION);
> if(durationObj instanceof String){
> ag.duration = new Integer((String) durationObj);
> } else {
> ag.duration = (Integer) durationObj;
> }
|
Code Block |
---|
|
<?php
/**
* @author Robin Bramley, Opsera Ltd
*/
require_once 'Stomp.php';
$c = new StompConnection("tcp://localhost:61626");
$result = $c->connect("smx", "smx");
print_r($result);
$c->subscribe("/queue/demo.org.servicemix.output");
// the JMS client uses a MapMessage so here we set the payload parameters as properties in the header
// along with the reply-to queue and a correlation id for good measure
$c->send("/queue/demo.org.servicemix.source", "",
array("ssn"=>"012-24932-53254",
"amount"=>12345.12,
"duration"=>36,
"reply-to"=>"/queue/demo.org.servicemix.output",
"correlation-id"=>"111111456"));
// Wait for the message to come in..
$result = $c->readFrame();
print_r($result);
// acknowledge the message so it is removed from the queue
$c->ack($result);
$c->disconnect();
?>
|
Code Block |
---|
title | Sample response message output |
---|
|
stompframe Object
(
[command] => MESSAGE
[headers] => Array
(
[bank] => bank3
[destination] => /queue/demo.org.servicemix.output
[type] => null
[reply-to] => null
[timestamp] => 1170954845203
[priority] => 4
[expires] => 0
[correlation-id] => 111111456
[rate] => 3.977056214363132
[message-id] => ID:rbramley-3265-1170951585062-6:0:2:1:9
)
[body] =>
)
|
|
Info |
---|
Building on steps 1-4 from above and using a Perl client... - Download Net-Stomp Perl module from: http://search.cpan.org/dist/Net-Stomp/
- Download Class-Accessor module from: http://search.cpan.org/~kasei/Class-Accessor-0.30/
- Make the libraries and copy to an appropriate location
- Put the following Perl code into LoanBrokerTest.pl
- Run: perl LoanBrokerTest.pl (sample response output below)
Code Block |
---|
|
# Loan broker test - Robin Bramley, Opsera Ltd
use Net::Stomp;
# set up connection
my $stomp = Net::Stomp->new( { hostname => 'localhost', port => '61626' } );
$stomp->connect( { login => 'smx', passcode => 'smx' } );
# subscribe to output channel
$stomp->subscribe(
{ destination => '/queue/demo.org.servicemix.output',
'ack' => 'client',
'activemq.prefetchSize' => 1
}
);
# send to source channel
$stomp->send(
{ destination => '/queue/demo.org.servicemix.source',
body => 'test message',
'ssn'=>'012-24932-53254',
'amount'=>12345.12,
'duration'=>36,
'reply-to'=>'/queue/demo.org.servicemix.output',
'correlation-id'=>'111111456' } );
# pick up the response
my $frame = $stomp->receive_frame;
print $frame->as_string;
$stomp->ack( { frame => $frame } );
# disconnect
$stomp->disconnect;
|
Code Block |
---|
title | Sample response message output |
---|
|
MESSAGE
priority:4
destination:/queue/demo.org.servicemix.output
correlation-id:111111456
reply-to:null
rate:4.940422499994942
bank:bank4
timestamp:1171540180281
type:null
message-id:ID:rbramley-2033-1171539209750-6:0:2:1:4
expires:0
|
|