jBPM Loops Concept

Hello all, As told i will be posting jBPM tutorials from now, here is my next tutorial on jBPM loops. prior to this tutorial please download jBPM 5.x version and install the demo environment after that open the eclipse jBPM editor and code the process as shown below. just by drag and drop the BPM components.

jbpm-looping

once the process is ready please click on the empty space of the editor and edit the properties as below.

process_init_var

now click the “first log node” as shown below and open the property Palette edit the action Values as shown below.
the “kcontext” is the jBPM KnowledgeContext available inorder to access the process variables.
Edit_log1

in the process shown there are two gateways the first one is converging gateway and the second one is diverging gateway. i will post a seperate article on gateways later.click on the first gateway and assign XOR as value for the type value. now click the second log node and in the property pallete edit the properties and put the simple logic as shown below.

Edit_log2

Now click on the second gateway since it is the converging gateway, click the Condition browse button and add a condition as shown below.

ifCondition

now lets give the else logic as shown below.else

once done lets prompt the process that it is done by editing the last log node.
now its time for a test run of the process

package com.spark.jbpm.sample.loops;

import java.util.HashMap;
import java.util.Map;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

/**
 * @author Sony
 *
 */
public class LoopingExample {
	
	public static final void main(String[] args) {
		try {
			// load up the knowledge base
			KnowledgeBase kbase = readKnowledgeBase();
			StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
			KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newThreadedFileLogger(ksession, "test", 1000);
			// start a new process instance
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("count", 5);
			ksession.startProcess("com.sample.looping", params);
			logger.close();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("looping/Looping.bpmn"), ResourceType.BPMN2);
		return kbuilder.newKnowledgeBase();
	}

}

and finally u will see the O/P of your first jBPM Process.

Happy jBPM πŸ™‚

Advertisements

Configuring memcached and Servlet Client

After some research I found that there is a Win32 binary version of memcached available here.

So go there and download win32 binary version to your computer. When downloaded, extract the file to folder memcahced on your C partition.

You should get only one file named memcached.exe. Now we need to install this as a service, as memCached is daemon and should be run only as service.

To install Memcached as a service, follow these steps (please change all forward slashes to backslashes):

1.If you’re running Vista, you should set memcached.exe to Run as administrator,Install the service using the command:
2.Install the service using the command:

c:/memcached/memcached.exe  -d install

3.Start the server from the Services or by running one of the following commands:

c:/memcached/memcached.exe -d  start

or

net start "memcached Server"

That is it, now your memcached server is installed and is listening to port 11211. There is one more thing left to do. By default, server is set to 64Mb memory limit, which is too small.

Once the configuration is done its time for us to create a client, that will interact with memcached server.

package com.spark.javaee6.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetSocketAddress;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.spy.memcached.MemcachedClient;

/**
 * Servlet implementation class Javaee6Servlet
 */
@WebServlet("/Javaee6Servlet")
public class Javaee6Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Javaee6Servlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		MemcachedClient c=new MemcachedClient(
			    new InetSocketAddress("localhost", 11211));

			// Store a value (async) for one hour
			c.set("sample_key", 3600, "sample_value");

		PrintWriter printWriter = response.getWriter();
		printWriter.println("session key: sample_key, session value: sample_value");
	}

}

in the line 45 the cache is storing a key,value pair. Now if want to retrieve it the following code has to be integrated.

MemcachedClient c=new MemcachedClient(
			    new InetSocketAddress("localhost", 11211));

			// Store a value (async) for one hour
		String value =  (String)c.get("sample_key");

That is it.
Happy Coding πŸ™‚

Building Web services on JBoss AS 7

Hi guys, here is my post regarding how to build web services on JBoss AS 7.x version. This post is fully self explanatory please follow the comments on the code.

step 1: create dynamic web project in eclipse with Jboss AS 7 as runtime configuration
step 2: in “src” folder code an interface as follows.

/**
 *
 */
package com.spark.webservice;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * @author Mantha Pavan Kumar
 *
 */
@WebService
public interface TemperatureConverter {

	@WebMethod
	public double convertCelsiusToFahrenheit(double value);
	@WebMethod
	public double convertFahrenheitToCelsius(double value);
}

step 3: code an implementation of the above interface as below.

/**
 *
 */
package com.spark.webservice.impl;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.spark.webservice.TemperatureConverter;

/**
 * @author Mantha Pavan Kumar
 *
 */
@WebService(endpointInterface="com.spark.webservice.TemperatureConverter",serviceName = "TemperatureConverter")
public class TemperatureConverterImpl implements TemperatureConverter{

	@Override
	@WebMethod
	public double convertCelsiusToFahrenheit(double value) {
		return value*(9/5)+32;
	}

	@Override
	@WebMethod
	public double convertFahrenheitToCelsius(double value) {
		return (value-32)*(5/9);
	}

}

step 4: now we deploy our web service as servlet on jboss, so lets code web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="jbosswebservices" version="2.5">
	
	<servlet>
		<servlet-name>webService</servlet-name>
		<servlet-class>com.spark.webservice.impl.TemperatureConverterImpl</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>webService</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

</web-app>

step 5: once step 4 is done lets deploy it in the JBoss AS 7 in the “standalone/deployments” folder
step 6: start the server:

Note:In order to use Web services in the new application server release you need the following extension in your configuration file:

<extension module="org.jboss.as.webservices"/>	

If you are using JBoss AS 7.1.1 this extension is already included in the standalone.xml configuration file, otherwise earlier releases include it in the standalone-preview.xml configuration files so for example if you want to start the standalone server and user webservices you need to use the –server-config option. For example:

standalone.bat --server-config=standalone-preview.xml

once deployed and the server started without any error open the server admin-console, in the web services menu u should see as below

Name Context
webService JBossWebServices

Web Service


Name: webService Context: JBossWebServices
Class: TemperatureConverterImpl Type: JAXWS_JSE
WSDL Url: http://localhost:8080/JBossWebServices?wsdl

Now its time to code the web service client.

step 1: download the following jar files:
a)cxf-bundle-2.0.8.jar,
b)org.apache.servicemix.bundles.jaxb-impl-2.1.6_1.jar,
c)org.apache.servicemix.bundles.xmlresolver-1.2_1.jar,
d)org.apache.servicemix.bundles.xmlschema-1.4.3_1.jar.

and call the web service as below.

/**
 * 
 */
package com.spark.webservice.test;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.spark.webservice.TemperatureConverter;


/**
 * @author Mantha Pavan Kumar
 *
 */
public class TemperatureConverterTest {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
		factoryBean.getInInterceptors().add(new LoggingInInterceptor());
		factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
		factoryBean.setServiceClass(TemperatureConverter.class);
		factoryBean.setAddress("http://localhost:8080/JBossWebServices");
		TemperatureConverter converter = (TemperatureConverter)factoryBean.create();
		System.out.println(converter.convertCelsiusToFahrenheit(10));
	}
}

The out put should look as below:

Mar 11, 2013 2:47:40 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://webservice.spark.com/}TemperatureConverterService from class com.spark.webservice.TemperatureConverter
Mar 11, 2013 2:47:42 PM org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
INFO: Outbound Message
---------------------------
Encoding: UTF-8
Headers: {SOAPAction=[""], Accept=[*]}
Messages: 
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:convertCelsiusToFahrenheit xmlns:ns1="http://webservice.spark.com/"><arg0>10.0</arg0></ns1:convertCelsiusToFahrenheit></soap:Body></soap:Envelope>
--------------------------------------
Mar 11, 2013 2:47:44 PM org.apache.cxf.interceptor.LoggingInInterceptor logging
INFO: Inbound Message
----------------------------
Encoding: UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Date=[Mon, 11 Mar 2013 09:17:44 GMT], Content-Length=[252], Server=[Apache-Coyote/1.1]}
Messages: 
Message:

Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:convertCelsiusToFahrenheitResponse xmlns:ns2="http://webservice.spark.com/"><return>42.0</return></ns2:convertCelsiusToFahrenheitResponse></soap:Body></soap:Envelope>
--------------------------------------
42.0

Happy Coding πŸ™‚