Author: hiranya
Date: Mon Dec 27 12:57:49 2010
New Revision: 1053061
URL: http://svn.apache.org/viewvc?rev=1053061&view=rev
Log:
Class mediator samples
Modified:
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples.xml
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample380.xml
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample381.xml
Modified:
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples.xml
URL:
http://svn.apache.org/viewvc/synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples.xml?rev=1053061&r1=1053060&r2=1053061&view=diff
==============================================================================
--- synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples.xml
(original)
+++ synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples.xml
Mon Dec 27 12:57:49 2010
@@ -159,8 +159,8 @@
<h4>Class Mediator (Writing Mediation Logic in Java)</h4>
<p>
<ul>
- <li><a href="#Sample380">Sample 380: Writing your own
custom mediation in Java</a></li>
- <li><a href="#Sample381">Sample 381: Implementing CBR
for binary messages using the class mediator</a></li>
+ <li><a href="samples/sample380.html">Sample 380:
Writing custom mediation logic in Java</a></li>
+ <li><a href="samples/sample381.html">Sample 381: Class
mediator for CBR of binary messages</a></li>
</ul>
</p>
<h4>XQuery Mediator</h4>
Modified:
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample380.xml
URL:
http://svn.apache.org/viewvc/synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample380.xml?rev=1053061&r1=1053060&r2=1053061&view=diff
==============================================================================
---
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample380.xml
(original)
+++
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample380.xml
Mon Dec 27 12:57:49 2010
@@ -23,7 +23,7 @@
<title>Apache Synapse - Sample 380</title>
</properties>
<body>
- <section name="Sample 380: ">
+ <section name="Sample 380: Writing Custom Mediation Logic in Java">
<div class="xmlConf"><definitions
xmlns="http://ws.apache.org/ns/synapse">
<sequence name="fault">
@@ -54,7 +54,8 @@
</definitions></div>
<subsection name="Objective">
<p>
-
+ Demonstrate the use of class mediator to extend the
mediation functionality of
+ Synapse
</p>
</subsection>
<subsection name="Pre-requisites">
@@ -74,11 +75,128 @@
</p>
</subsection>
<subsection name="Executing the Client">
- <div class="command">ant stockquote
-Daddurl=http://localhost:9000/services/SimpleStockQuoteService
-Dtrpurl=http://localhost:8280/</div>
+ <p>
+ In this configuration, Synapse hands over the request
message to the specified
+ endpoint, which sends it to the Axis2 server running on
port 9000. But the response
+ message is passed through the class mediator before
sending it back to the client.
+ Class mediator in turns hands over the message to the
specified Java class for
+ further processing. In that regard, the class mediator
acts as a delegating
+ mediator which delegates the message to a custom Java
class for processing.
+ Two parameters named 'discountFactor' and 'bonusFor' are
passed to the mediator
+ implementation class (i.e.
samples.mediators.DiscountQuoteMediator) before each
+ invocation. Source of the mediator implementation class is
shown below.
+ </p>
+ <div class="consoleOutput">package samples.mediators;
+
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.Mediator;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.xml.namespace.QName;
+
+public class DiscountQuoteMediator implements Mediator {
+
+ private static final Log log =
LogFactory.getLog(DiscountQuoteMediator.class);
+
+ private String discountFactor="10";
+
+ private String bonusFor="10";
+
+ private int bonusCount=0;
+
+ public DiscountQuoteMediator(){}
+
+ public boolean mediate(MessageContext mc) {
+
+ String price=
mc.getEnvelope().getBody().getFirstElement().getFirstElement().
+ getFirstChildWithName(new
QName("http://services.samples/xsd","last")).getText();
+
+ //converting String properties into integers
+ int discount=Integer.parseInt(discountFactor);
+ int bonusNo=Integer.parseInt(bonusFor);
+ double currentPrice=Double.parseDouble(price);
+
+ //discounting factor is deducted from current price form every response
+ Double lastPrice = new Double(currentPrice - currentPrice * discount /
100);
+
+ //Special discount of 5% offers for the first responses as set in the
bonusFor property
+ if (bonusCount <= bonusNo) {
+ lastPrice = new Double(lastPrice.doubleValue() -
lastPrice.doubleValue() * 0.05);
+ bonusCount++;
+ }
+
+ String discountedPrice = lastPrice.toString();
- <div class="consoleOutput">Sat Nov 18 21:01:23 IST 2006
SimpleStockQuoteService :: Generating quote for : IBM</div>
+
mc.getEnvelope().getBody().getFirstElement().getFirstElement().getFirstChildWithName
+ (new
QName("http://services.samples/xsd","last")).setText(discountedPrice);
+ System.out.println("Quote value discounted.");
+ System.out.println("Original price: " + price);
+ System.out.println("Discounted price: " + discountedPrice);
+
+ return true;
+ }
+
+ public String getType() {
+ return null;
+ }
+
+ public void setTraceState(int traceState) {
+ traceState = 0;
+ }
+
+ public int getTraceState() {
+ return 0;
+ }
+
+ public void setDiscountFactor(String discount) {
+ discountFactor=discount;
+ }
+
+ public String getDiscountFactor() {
+ return discountFactor;
+ }
+
+ public void setBonusFor(String bonus){
+ bonusFor=bonus;
+ }
+
+ public String getBonusFor(){
+ return bonusFor;
+ }
+}</div>
+ <p>
+ All classes developed for the class mediator should
implement the 'Mediator'
+ interface, which contains the mediate(...) method. The
mediate(...) method of the
+ above class is invoked for each response message mediated
through the main
+ sequence, with the message context of the current message
as the parameter. All
+ details of the message including the SOAP headers, SOAP
body and properties of
+ the context hierarchy can be accessed from the message
context. In this sample,
+ the body of the message is retrieved and the discount
percentage is subtracted
+ from the quote price. If the quote request number is less
than the number specified
+ in the 'bonusFor' property in the configuration, a special
discount is given.
+ </p>
+ <p>
+ To test the custom code and the class mediator, invoke the
test client as
+ follows.
+ </p>
+ <div class="command">ant stockquote -Dsymbol=IBM -Dmode=quote
-Daddurl=http://localhost:8280</div>
+ <p>
+ You will see the below output in the client console with
the discounted quote value.
+ </p>
<div class="consoleOutput">Standard :: Stock price =
$95.26454380258552</div>
+ <p>
+ If you check the Synapse console, you will notice the
messages printed by the
+ custom mediator during mediation.
+ </p>
+ <div class="consoleOutput">Quote value discounted.
+Original price: 162.30945327447262
+Discounted price: 138.77458254967408</div>
</subsection>
</section>
<p><a href="../samples.html">Back to Catalog</a></p>
Modified:
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample381.xml
URL:
http://svn.apache.org/viewvc/synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample381.xml?rev=1053061&r1=1053060&r2=1053061&view=diff
==============================================================================
---
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample381.xml
(original)
+++
synapse/trunk/scratch/hiranya/website/src/site/xdoc/userguide/samples/sample381.xml
Mon Dec 27 12:57:49 2010
@@ -23,7 +23,7 @@
<title>Apache Synapse - Sample 381</title>
</properties>
<body>
- <section name="Sample 381: ">
+ <section name="Sample 381: Class Mediator for CBR of Binary Messages">
<div class="xmlConf"><definitions
xmlns="http://ws.apache.org/ns/synapse">
<proxy name="JMSBinaryProxy" transports="jms">
@@ -68,14 +68,20 @@
</definitions></div>
<subsection name="Objective">
<p>
-
+ Demonstrate an advanced content based routing (CBR)
scenario using a custom mediator
</p>
</subsection>
<subsection name="Pre-requisites">
<p>
- <ul>
+ <ul>
+ <li>
+ Setup and start a JMS broker (Apache ActiveMQ can
be used as the
+ JMS broker for this scenario. Refer JMS setup
guide for information on
+ how to run ActiveMQ.)
+ </li>
<li>
- Deploy the SimpleStockQuoteService in the sample
Axis2 server and start Axis2
+ Enable the JMS transport receiver and sender of
Synapse (Refer JMS setup
+ guide for more details)
</li>
<li>
Start Synapse using the configuration numbered 381
(repository/conf/sample/synapse_sample_381.xml)
@@ -88,11 +94,39 @@
</p>
</subsection>
<subsection name="Executing the Client">
- <div class="command">ant stockquote
-Daddurl=http://localhost:9000/services/SimpleStockQuoteService
-Dtrpurl=http://localhost:8280/</div>
-
- <div class="consoleOutput">Sat Nov 18 21:01:23 IST 2006
SimpleStockQuoteService :: Generating quote for : IBM</div>
-
- <div class="consoleOutput">Standard :: Stock price =
$95.26454380258552</div>
+ <p>
+ In this configuration, a proxy service has been defined to
accept incoming JMS
+ messages. JMS messages contain binary payloads. User
configures the offset, length,
+ and binary encoding of the text literal that Synapse
should use for CBR. Configuration
+ simply routes the messages based on this text to different
endpoints.
+ </p>
+ <p>
+ A JMS producer and two instances of a consumer used to
demonstrate the CBR functionality.
+ </p>
+ <p>
+ Now run the first consumer using the following command.
+ </p>
+ <div class="command">ant mddconsumer -Djms_topic=mdd.MSFT</div>
+ <p>
+ Now run the second consumer using the following command.
+ </p>
+ <div class="command">ant mddconsumer -Djms_topic=mdd.GOOG</div>
+ <p>
+ Now run the market data producer to genenrate market data
for symbol 'MSFT' using
+ the following command.
+ </p>
+ <div class="command">ant mddproducer -Dsymbol=MSFT</div>
+ <p>
+ Now run the market data producer to genenrate market data
for symbol 'GOOG' using
+ the following command.
+ </p>
+ <div class="command">ant mddproducer -Dsymbol=GOOG</div>
+ <p>
+ You will see the below output in the client console(s)
based on the symbol.
+ </p>
+ <div class="consoleOutput">mddconsumer:
+ [java] Market data recived for symbol : topic://mdd.MSFT
+ [java] Market data recived for symbol : topic://mdd.MSFT</div>
</subsection>
</section>
<p><a href="../samples.html">Back to Catalog</a></p>