hi all,

i've put together an example of a long running business process in bpmscript
that showcases some of the things you can do with bpmscript (on top of
servicemix, of course). The script looks like this:

/**
* Demonstration of BpmScript using services including:
*
* - Lightweight Email Component
* - Lightweight SOAP Component (modified)
* - BpmScript Alarm Service
*
* Example takes in a request which looks something like:
* <request>
*  <symbol>GOOG</symbol>
*  <min>12.9</min>
*  <max>53.9</max>
*  <to>[EMAIL PROTECTED]</to>
* </request>
*
* It then looks up the stock price associated with the
* symbol each hour using the ws.investbox.com web service
* and email's the person specified in the to field of the
* request if the price reaches more than the max or less
* than the min. If not, the script sleeps for an hour and
* then tries again.
**/
function main(input, sender) {

   // parse out the source xml
   var request = sourceToXml(input.getInMessage().getContent());

   // create some BpmScript Services
   var soapService = new SoapService(sender);
   var alarmService = new AlarmService(sender);
   var emailService = new EmailService(sender);

   // grab the minimum and maximum values from the request
   var min = parseFloat(request.min.text().toString());
   var max = parseFloat(request.max.text().toString());

   // check every hour
   while(alarmService.sendSync("PT1H")) {

     // create the SOAP content
     var message = <tns:GetQuote xmlns:tns="http://ws.invesbot.com/";>
       <tns:symbol>{request..symbol.text()}</tns:symbol>
       </tns:GetQuote>;

     // send out the soap request
     var exchange = soapService.sendSync(
       soapService.toURL("http://ws.invesbot.com/stockquotes.asmx";),
       "http://ws.invesbot.com/GetQuote";,
       xmlToSource(message));

     // get the xml from the response
     var xml = sourceToXml(exchange.getOutMessage().getContent());

     // parse out the bid. the reason this is more complicated
     // than it needs to be is because the data comes back as e.g.
23.5<small>x 100</small>
     var bid =
parseFloat(/^(\d+\.\d+)/.exec(xml..Bid.text().toString())[0]);

     // send out an email if the bid price is higher than the max
     // or lower than the minimum set and end the script
     if(bid < min || bid > max) {
       emailService.send({"to":request.to.text().toString(),
         "from":request.to.text().toString(),
         "subject":"Bid has reached " + (bid < min ? "minimum" : "maximum")
+ " value of " + bid,
         "text":xml.toXMLString()});
       break;
     }
 }

 sender.reply(ST.toStreamSource("<done/>"));

}


The spring config for the example looks as follows (without the smtp
properties -- i was testing using gmail):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:sm="http://servicemix.apache.org/config/1.0";
 xmlns="http://xbean.org/schemas/spring/1.0";
 xmlns:bs="http://bpmscript.org/jbi"; xmlns:test="urn:test">

 <bean id="mailSender"
   class="org.springframework.mail.javamail.JavaMailSenderImpl">
   <property name="host" value="${smtp.host}" />
   <property name="port" value="${smtp.port}" />
   <property name="protocol" value="smtps" />
   <property name="username" value="${smtp.username}" />
   <property name="password" value="${smtp.password}" />
   <property name="javaMailProperties">
     <props>
       <prop key="mail.smtps.auth">true</prop>
       <prop key="mail.smtp.starttls.enable">true</prop>
       <prop key="mail.smtp.debug">true</prop>
     </props>
   </property>
 </bean>

 <sm:container id="jbi" embedded="true" flowName="seda"
   createMBeanServer="false">

   <sm:activationSpecs>

     <sm:activationSpec>
       <sm:component>
         <bean
           class="
org.bpmscript.jbi.component.InMemoryBpmScriptSpringComponent">
           <property name="endpoints">
             <list>
               <bean
                 class="org.bpmscript.jbi.component.ProcessEndpoint">
                 <property name="endpoint" value="email" />
                 <property name="sourceResource"
                   value="classpath:email/main.js" />
               </bean>
             </list>
           </property>
         </bean>
       </sm:component>
     </sm:activationSpec>

     <sm:activationSpec componentName="dynamicsoap"
       service="bs:dynamicsoap">
       <sm:component>
         <bean class="org.bpmscript.jbi.DynamicSaajBinding"></bean>
       </sm:component>
     </sm:activationSpec>

     <sm:activationSpec componentName="emailSender"
       service="bs:emailSender">
       <sm:component>
         <bean
           class="org.apache.servicemix.components.email.SimpleMailSender">
           <property name="sender" ref="mailSender"/>
         </bean>
       </sm:component>
     </sm:activationSpec>

   </sm:activationSpecs>
 </sm:container>

</beans>

Reply via email to