Laszlo – Simple Web Service

September 19, 2006

This post shows a simple example of a Laszlo web service. It uses the xfire web service created in this post. The user enters a symbol and clicks Get Quote, laszlo makes a call to the webservice to retrieve a quote for the symbol. The laszlo documentation describes the use of soap in laszlo applications.

This is a screenshot of the laszlo page:

Stock Quote Screen Shot

<canvas debug="true">

<soap name="stockquote"
  wsdl="http://localhost:8080/xfire-stockquote/services/StockQuote?wsdl">

<!-- Method to make a document for SOAP message requests -->
<method name="makegetQuoteDoc" args="symbol">
	Debug.write('Entering makegetQuoteDoc function');
<![CDATA[
    var s =  '<getStockQuote xmlns="http://localhost:8080/xfire-stockquote/services/stockQuote" ><security>'+symbol+'</security></getStockQuote>';

Debug.write(s);
    return s;
]]>
</method>

<handler name="onload">
    // make buttons visible once SOAP object is loaded
		canvas.buttons.setAttribute('visible', true);
		Debug.write('StockQuote soap service loaded');
    Debug.write('StockQuote WSDL at ' + this.wsdl);
    Debug.write('proxy:');
    Debug.inspect(this.proxy);
</handler>

<handler name="onerror" args="error">
    Debug.write('error:', error);
</handler>

<handler name="ontimeout" args="error">
    Debug.write('timeout:', error);
</handler>

<remotecall funcname="getStockQuote" >
  <param value="${ canvas.stockquote.makegetQuoteDoc(symbol.text) }" />
  <method event="ondata" args="value">
		    Debug.write("Got data!");
		    result.setText(value);
  </method>
</remotecall>

</soap>

<view name="buttons" x="10" y="10" visible="false" layout="spacing: 10" >
<text><b>Stock Quote Service</b></text>
<view layout="axis: x" ><text y="3">Symbol:</text><edittext id="symbol" text="IBM"/></view>
<view layout="axis: x" ><text>Result:</text><text id="result"/></view>

<button text="Get Quote" onclick="canvas.stockquote.getStockQuote.invoke()" />

</view>

</canvas>

Web Service with Spring and XFire

September 12, 2006

In a previous post I looked at how a simple quote web service with XFire. This post will implement the same webservice using XFire and Spring.

WEB-INF/web.xml

Configure the xfire dispatcher servlet and the spring config files in the web.xml

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/applicationContext.xml
	classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
</context-param>

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
	<servlet-name>xfire</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>xfire</servlet-name>
	<url-pattern>/*</url-pattern>
</servlet-mapping>

WEB-INF/applicationContext.xml

Configure the service class in the applicationContext.xml file:

<beans>
    <bean id="stockQuoteService" class="demo.xfire.service.StockQuoteServiceImpl"/>
</beans>

WEB-INF/xfire-servlet.xml

Configure a SimpleUrlHandlerMapping and an XFireExporter to expose the methods of our service class:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="urlMap">
		<map>
			<entry key="/StockQuoteService">
				<ref bean="stockQuote"/>
			</entry>
		</map>
	</property>
</bean>

<bean id="stockQuote" class="org.codehaus.xfire.spring.remoting.XFireExporter">
	<property name="serviceFactory">
		<ref bean="xfire.serviceFactory"/>
	</property>
	<property name="xfire">
		<ref bean="xfire"/>
	</property>
	<property name="serviceBean">
		<ref bean="stockQuoteService"/>
	</property>
	<property name="serviceClass">
		<value>demo.xfire.service.StockQuoteService</value>
	</property>
</bean>

Service interface

public interface StockQuoteService {
  public Float getStockQuote(String security);
}

Service Implementation

public class StockQuoteServiceImpl {

protected final Log log = LogFactory.getLog(getClass());

public Float getStockQuote(String security)
  {
    Random rand = new Random();
    log.debug("Retrieving quote for "+ security);
    return new Float(rand.nextInt(101));
  }
}

Testing

After the demo application has been deployed the url will be similar to http://localhost:8080/xfire-spring-demo/StockQuoteService?wsdl for tomcat.

To run requests against the web service you could use soapUI.

The Code

You can download the war file with the code here xfire-spring-stockquote.war

If you are not using Tomcat you need to change the location of the log file in the log4j.properties file.


Using Properties Files in Mule Configuration

September 7, 2006

In a previous post I looked at how to use quartz to trigger events in mule – we had a cron expression which dictated when an event occurred. This post explains how to externalise this cron expression to a properties file.

We create a properties file (application.properties) with the cron expression. This properties file should be on the classpath.

cron.expression=0 24 15 * * ?

First we reference our properties file in the mule config:

<environment-properties>
  <file-properties location="application.properties"/>
</environment-properties>

We can then use the same syntax as Spring to acces the parameter in the properties file:

<endpoint name="quartz.in" address="quartz:/myService">
  <properties>
    <property name="cronExpression" value="${cron.expression}" />
  </properties>
</endpoint>

This example shows how to externalise configuration properties to a properties file.


Using environment variable to control the location of log files in Tomcat

September 6, 2006

You can control the location of log files in Tomcat using the ${catalina.home} environment variable.

Example:

log4j.rootLogger=DEBUG, file

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MMM HH:mm:ss.SSS} %p - %C.%M(%L) | %m%n
log4j.appender.file.File=${catalina.home}/logs/applicationname.log

Log4j – Reporting application errors by email

September 1, 2006

This post shows how you can setup log4j to send email when errors occur in your application. log4j.properties extract:

# Root category priority: DEBUG, Appender:mail
log4j.rootCategory=debug, mail

# Configuration for receiving e-mails when ERROR messages occur.
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.To=admin@company.com
log4j.appender.mail.From=error@application-name.com
log4j.appender.mail.SMTPHost=smtp.company.com
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.BufferSize=1
log4j.appender.mail.Subject=Application Error Occurred
log4j.appender.mail.layout=org.apache.log4j.HTMLLayout

Using Quartz to trigger events in Mule

September 1, 2006

This posts shows how to use Quartz to trigger events in Mule.
Simple Trigger

Simple triggers allows you to execute a component repeatedly after a number of miliseconds. This component will be triggered 20 seconds after the application starts (startDelay=20000) and then every 10 seconds (repeatInterval=10000)

<global-endpoints>
  <endpoint name="quartz.in" address="quartz:/myService">
    <properties>
      <property name="repeatInterval" value="10000" />
      <property name="startDelay" value="20000" />
      <property name="payloadClassName" value="java.lang.String" />
    </properties>
  </endpoint>
</global-endpoints>

<mule-descriptor name="sampleComponent"
	inboundEndpoint="quartz.in"
	implementation="demo.mule.example7.SampleComponent"/>

Cron Trigger

Cron trigger allows us to execute a component based on a cron trigger (more information on cron triggers can be found here ). This component will be triggered every day at 8:00.

<global-endpoints>
  <endpoint name="quartz.in" address="quartz:/myService">
    <properties>
      <property name="cronExpression" value="0 00 08 * * ?" />
    </properties>
  </endpoint>
</global-endpoints>

<mule-descriptor name="sampleComponent"
	inboundEndpoint="quartz.in"
	implementation="demo.mule.SampleComponent"/>