<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6033871083265669572</id><updated>2011-12-21T22:32:57.140Z</updated><category term='SOAP'/><category term='appending bytes to JSON'/><category term='JAX-RS support for JAXB'/><category term='JAX-RS support for JSON'/><category term='Axis'/><category term='REST'/><category term='Tulio'/><category term='Web Services'/><category term='Session Management'/><category term='Apache SOAP Framework'/><category term='Axis2 Client'/><category term='SOAP client'/><category term='JAX-RS'/><category term='HibernateTemplate'/><category term='Jersey'/><category term='Axis2 example'/><category term='Axis2 tutorial'/><title type='text'>Rambling on about J2EE development</title><subtitle type='html'>Articles and Tutorials</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tuliodomingos.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://tuliodomingos.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Tulio Domingos</name><uri>http://www.blogger.com/profile/14981659811312844527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://2.bp.blogspot.com/_nI0cE8KhCUs/S3arXSrk_bI/AAAAAAAAAAs/C7EZXDm6obc/S220/profile-glenfinnan.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6033871083265669572.post-799736340805762772</id><published>2011-06-03T13:50:00.004+01:00</published><updated>2011-06-08T11:35:59.157+01:00</updated><title type='text'>Performance Tuning in JBoss AS</title><content type='html'>JBoss is an open source Java EE application server widely used for web development, testing and  deployment. In this tutorial, I share some performance tips I recently applied on a number of JBoss servers for a telco company.&lt;br /&gt;&lt;br /&gt;Performance has a significant impact on user experience, business and productivity, not to mention savings on hardware and software resources. For details on performance optimization and its benefits, I recommend&lt;a target="_blank" href="http://www.javaworld.com/javaworld/jw-05-2004/jw-0517-optimization.html"&gt; this article.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Tuning is all about identifying bottlenecks! Performance bottlenecks are one of the nastiest to resolve: it could be due to poor AS configuration, bad coding, slow SQL statements, hardware limitations, etc. Surely though you'll aim to get the most out of your company's middleware investment :-) so upgrading the hardware is not an option. &lt;br /&gt;&lt;br /&gt;This tutorial applies to &lt;b&gt;JBoss version 4.x&lt;/b&gt;. Consider making a backup before changing any of your AS scripts.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;General information before we start &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;To recap: processors, operating systems and applications can all be of 32 or 64 bit, as a result we get 32/64-bit CPU, OS and JVMs. For a more detailed explanation &lt;a target="_blank" href="http://www.techsupportalert.com/content/32-bit-and-64-bit-explained.htm"&gt;check out this article&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If the processor (CPU) is 32-bit then the OS must also be 32-bit, however, if the CPU is 64-bit, then you can run both 32 and 64 bit OSes, just bear in mind that running a 32-bit OS on a 64-bit CPU means your memory can only go as high as 4GB (half of a 64-bit OS). In other words, The OS can only be installed if the hardware architecture supports it.&lt;br /&gt;&lt;br /&gt;to find out whether the CPU is 32 or 64 bit, run the following on your Linux shell: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;grep flags /proc/cpuinfo&lt;/code&gt;&lt;br /&gt;&lt;blockquote style="font-size:85%"&gt;&lt;span style="font-style:italic;"&gt;lm (long mode): 64-bit &lt;br /&gt;Protected mode: 32-bit&lt;br /&gt;Real mode: 16-bit&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;Now, check whether your OS is 32-bit or 64 bit. Again from  the Linux shell:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;uname -m&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Finally, try running your JVM in 64-bit mode: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;java -d64 -version&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;1. Tuning JVM parameters&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In JBoss, set your JAVA_HOME to point to your 64-bit JDK otherwise JBoss will probably run on its default 32-bit JVM. When you start JBoss, check the [Server Info] console log, It should say "64-bit Server VM", if not, you can activate the 64-bit JVM by adding the following inside &lt;i&gt;run.conf&lt;/i&gt; file: &lt;br /&gt;&lt;br /&gt;JAVA_OPTS="-D64..."&lt;br /&gt;&lt;br /&gt;The &lt;i&gt;run.conf&lt;/i&gt; file contains JVM parameters including memory configuration:&lt;br /&gt;&lt;br /&gt;set JAVA_OPTS="$JAVA_OPTS -Xms3g -Xmx3g"&lt;br /&gt;&lt;blockquote style="font-size:85%"&gt;&lt;b&gt;Note: &lt;/b&gt;&lt;span style="font-style:italic;"&gt;Be careful not to set it too high, you need to give space to the native JVM.&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;Consider having fixed heap settings (same value for -Xms and -Xmx). If, for example, you only set "-Xmx" then the VM starts with a much smaller heap and re-sizes it. The actual heap size setting will depend on your application requirements and server RAM.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2. Garbage Collection&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;By default, JBoss RMI subsystem runs a full garbage collection every minute. Whether you're using RMI or not, all threads are paused and the entire heap is scanned. This can take a good time specially under load. You can change this behaviour to once an hour in &lt;i&gt;run.conf&lt;/i&gt;. &lt;br /&gt;&lt;br /&gt;This is set in the same way as the JVM parameters: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;set JAVA_OPTS="... -Dsun.rmi.dgc.client.gcInterval=3600000 &lt;br /&gt;-Dsun.rmi.dgc.server.gcInterval=3600000"&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;3. Stack Memory&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Unlike the heap memory (whose size is controlled via -Xmx), the thread stack memory is not collected by the GC. JVM errors such as &lt;i&gt;StackOverflowError&lt;/i&gt; and &lt;i&gt;OutOfMemoryExceptions&lt;/i&gt; can also be related to the stack memory (where primitive vars and method call goes). Add the following to &lt;i&gt;run.conf&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;set JAVA_OPTS="... -XX:ThreadStackSize=256"&lt;/code&gt;&lt;br /&gt;&lt;blockquote style="font-size:85%"&gt;&lt;b&gt;Note: &lt;/b&gt;&lt;span style="font-style:italic;"&gt;256K is the size of the thread, you can set 128 for smaller apps&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;The default thread stack memory space on 64-bit JVMs is 1M and 512K for 32-bit JVMs. Refer to &lt;a target="_blank" href="https://community.atg.com/docs/DOC-1329"&gt;Java Thread Stack Sizing doc&lt;/a&gt; for more details.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;4. Turn off Debug Information in JSP Classes&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;By default, Apache's JSP compiler (Jasper) in JBoss, will compile JSP classes in debug mode. In production systems you should turn this off to increase the resources available to your application. &lt;br /&gt;&lt;br /&gt;Edit file &lt;code&gt;$JBOSS_HOME/server/default/jboss-web.deployer/conf/web.xml&lt;/code&gt; Add extra  initialisation parameter (&lt;code&gt;classdebuginfo&lt;/code&gt;) to the &lt;code&gt;JspServlet&lt;/code&gt;: &lt;br /&gt;&lt;br /&gt;&amp;lt;servlet&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;servlet-name&amp;gt;jsp&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;servlet-class&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;org.apache.jasper.servlet.JspServlet&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;init-param&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;param-name&amp;gt;classdebuginfo&amp;lt;/param-name&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;param-value&amp;gt;false&amp;lt;/param-value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;/init-param&amp;gt;&lt;br /&gt;&amp;lt;/servlet&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;5. Hot Deployment&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;It's recommended to disable Hot Deployment (HDScanner) in production as it can compromise performance and potentially expose security risks. Add the following in file: &lt;code&gt;$JBOSS_HOME/server/default/conf/jboss-service.xml&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&amp;lt;attribute name="ScanPeriod"&amp;gt;BIG_NUMBER_IN_MS&amp;lt;/attribute&amp;gt;&lt;/code&gt;&lt;br /&gt;&lt;blockquote style="font-size:85%"&gt;&lt;b&gt;Note: &lt;/b&gt;&lt;span style="font-style:italic;"&gt;Default is 5000ms (5 seconds)&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;Or alternatively, turn it off:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&amp;lt;attribute name="ScanPeriod"&amp;gt;false&amp;lt;/attribute&amp;gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;6. Binding JBoss to IP addresses&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;As of version 4.2, JBoss binds all its services to the 'localhost' interface (127.0.0.1). Which means it can only be accessed from the localhost. This change was done for security reasons, previously it was bound to any IP address (0.0.0.0) by default. &lt;br /&gt;&lt;br /&gt;On production, the default localhost interface (127.0.0.1) configuration is not practical as the server will be accessed by different client IPs. Below are 3 options you can utilise to bind JBoss services to a desired IP or any network interface (0.0.0.0):&lt;br /&gt;&lt;dl&gt;&lt;dt&gt;* Passing a parameter to the startup script&lt;/dt&gt;&lt;dd&gt;./run.sh -b0.0.0.0&lt;/dd&gt;&lt;dt&gt;* Editing &lt;code&gt;$JBOSS_HOME/bin/run.conf&lt;/code&gt;&lt;/dt&gt;&lt;dd&gt;Add the following at the end of the file: &lt;code&gt;JAVA_OPTS="$JAVA_OPTS -Djboss.bind.address=0.0.0.0"&lt;/code&gt;&lt;/dd&gt;&lt;dt&gt;* Editing &lt;code&gt;$JBOSS_HOME/server/default/deploy/jboss-web.deployer/server.xml&lt;/code&gt;&lt;/dt&gt;&lt;dd&gt;For all &lt;code&gt;&amp;lt;Connector&amp;gt;&lt;/code&gt; elements, add the following attribute &lt;code&gt;&amp;lt;Connector address="0.0.0.0"&lt;/code&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;b&gt; Summarising &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The above are just some of JBoss parameters you can play with. In my opinion, the most effective ones but for a comprehensive list of parameters please refer to the "Further Reading" section below. There's plenty of more stuff out there.&lt;br /&gt;&lt;br /&gt;Note that while optimizing your application, as soon as one area improves another can become a bottleneck, therefore it's important to keep testing the application under stress conditions for each and every change. Performance tuning is an iterative process, you must repeat the cycle until you're happy with the results. Such tests are accomplished using load test tools like &lt;a target="_blank" href="http://jakarta.apache.org/jmeter/"&gt;Apache JMeter&lt;/a&gt; and &lt;a target="_blank" href="http://www8.hp.com/us/en/software/software-product.html?compURI=tcm:245-935779"&gt;HP LoadRunner&lt;/a&gt;. I'm more familiar with &lt;a target="_blank" href="http://grinder.sourceforge.net/"&gt;Grinder&lt;/a&gt; but in case you're wondering which one to go for, &lt;a target="_blank" href="http://www.opensourcetesting.org/performance.php"&gt;here&lt;/a&gt; is a list of open source performance test tools. &lt;br /&gt;&lt;br /&gt;Hasta luego!&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Further Reading&lt;/b&gt;&lt;br /&gt;1. &lt;a target="_blank" style="color:ECEBEA" href="http://onjava.com/pub/a/onjava/2003/05/28/jboss_optimization.html"&gt;JBoss Optimizations 101&lt;/a&gt;&lt;br /&gt;2. &lt;a target="_blank" href="http://www.jboss.com/pdf/JB_JEAP5_PerformanceTuning_wp_web.pdf"&gt;Best practices for performance tuning&lt;/a&gt;&lt;br /&gt;3. &lt;a target="_blank" href="http://www.javaworld.com/javaworld/jw-06-2006/jw-0619-tuning.html"&gt;Solving common Java EE performance problems&lt;/a&gt;&lt;br /&gt;4. &lt;a target="_blank" href="https://support.eapps.com/index.php?/Knowledgebase/Article/View/207/1/user-guide---jboss-installation-and-tuning"&gt;User Guide - JBoss Installation and Tuning&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6033871083265669572-799736340805762772?l=tuliodomingos.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tuliodomingos.blogspot.com/feeds/799736340805762772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tuliodomingos.blogspot.com/2011/06/performance-tuning-in-jboss-as.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/799736340805762772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/799736340805762772'/><link rel='alternate' type='text/html' href='http://tuliodomingos.blogspot.com/2011/06/performance-tuning-in-jboss-as.html' title='Performance Tuning in JBoss AS'/><author><name>Tulio Domingos</name><uri>http://www.blogger.com/profile/14981659811312844527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://2.bp.blogspot.com/_nI0cE8KhCUs/S3arXSrk_bI/AAAAAAAAAAs/C7EZXDm6obc/S220/profile-glenfinnan.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6033871083265669572.post-3398296752128201854</id><published>2011-05-03T11:04:00.007+01:00</published><updated>2011-05-05T22:09:52.392+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='HibernateTemplate'/><category scheme='http://www.blogger.com/atom/ns#' term='Session Management'/><title type='text'>Hibernate Session Management in Spring</title><content type='html'>Once there was JDBC, then Hibernate, then HibernateTemplate...&lt;br /&gt;&lt;br /&gt;SpringSource came up with a set of template-based patterns (JdbcTemplate, HibernateTemplate, JpaTemplate, RestTemplate, etc) which basically takes care of all the necessary housekeeping, allowing you to concentrate on the logic of your application...anyways, I'm going to take a practical approach to hibernate session management in Spring using SpringHibernateTemplate.&lt;br /&gt;&lt;br /&gt;Before we start, it's important to note that as of Spring 3 documentation, &lt;b&gt;it is no longer recommended to use HibernateTemplate&lt;/b&gt; as it unnecessarily ties your code to Spring classes.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;SpringHibernateTemplate&lt;/i&gt; is a Spring wrapper around the native Hibernate API. It adds an extra layer of abstraction on top of Hibernate making life a bit easier specially when it comes to session management.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3 Basic Steps:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;1. Hibernate Mapping File:&lt;/span&gt; &lt;br /&gt;Create the standard hibernate mapping file(s) for your class bean(s) just as you would on any Hibernate project, nothing interesting here, business as usual.&lt;br /&gt;&lt;br /&gt;&amp;ensp;&amp;ensp;• DB.CUSTOMER &gt;&gt; Customer.java &gt;&gt; Customer.hbm.xml&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2. DAO Implementation:&lt;/span&gt;&lt;br /&gt;Here is where it all starts, define a second class &lt;code style="font-size:90%"&gt;CustomerSpringDao&lt;/code&gt; that extends &lt;code style="font-size:90%"&gt;HibernateDaoSupport&lt;/code&gt; so we can include database operations.&lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;public class CustomerSpringDao extends HibernateDaoSupport&lt;br /&gt;{&lt;br /&gt;&amp;ensp;public void addCustomer(Customer customer) {&lt;br /&gt;&amp;ensp;&amp;ensp;getHibernateTemplate().save(customer);&lt;br /&gt;&amp;ensp;}&lt;br /&gt;&lt;br /&gt;&amp;ensp;public Customer getCustomerAccountInfo(Customer customer) {&lt;br /&gt;&amp;ensp;&amp;ensp;Customer cust = null;&lt;br /&gt;&amp;ensp;&amp;ensp;List list = getHibernateTemplate().find("from Customer customer where customer.userId = ?" , customer.getUserId(),Hibernate.STRING); &lt;br /&gt;&lt;br /&gt;&amp;ensp;&amp;ensp;if(list.size() &gt; 0){&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;cust = (Customer)  list.get(0);&lt;br /&gt;&amp;ensp;&amp;ensp;}&lt;br /&gt;&lt;br /&gt;&amp;ensp;&amp;ensp;return cust;&lt;br /&gt;&amp;ensp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;b&gt;&lt;code style="font-size:90%"&gt;HibernateDaoSupport&lt;/code&gt;&lt;/b&gt;&lt;br /&gt;This is a convenient super class for Hibernate-based data access objects. By extending this Spring abstract class you can make use of methods such as &lt;code style="font-size:90%"&gt;getHibernateTemplate()&lt;/code&gt; where you can perform hibernate operations without having to manage sessions manually. The SessionFactory is pre-initialized by the framework. Yep, that's right, it is completely abstracting the &lt;code style="font-size:90%"&gt;sessionFactory.openSession()&lt;/code&gt;, &lt;code style="font-size:90%"&gt;session.flush()&lt;/code&gt;, etc.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3. JDBC DataSource and HibernateSessionFactory Wiring&lt;/span&gt; &lt;br /&gt;In the Spring framework, resources such as JDBC DataSource and HibernateSessionFactory can be realised as beans in the application context. Create the Spring Application Context file (if you haven't already created one) and place it in the classpath of your project. This file should reference your &lt;code style="font-size:90%"&gt;CustomerSpringDao&lt;/code&gt; class.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Putting it all toghether&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;Once you've completed all 3 steps, you will be ready to put it all into action. The following is an example on how you can access those spring beans and make use of spring's session management. &lt;br /&gt;&lt;br /&gt;Before anything else, you can load the Spring application context XML file into a BeanFactory using the ClassPathXmlApplicationContext class, then go ahead persisting your object using common hibernate methods:&lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;ClassPathXmlApplicationContext context = new&lt;br /&gt;ClassPathXmlApplicationContext("context.xml");&lt;br /&gt;&lt;br /&gt;CustomerSpringDao csd = (CustomerSpringDao) context.getBean("eventDao", CustomerSpringDao.class);&lt;br /&gt;&lt;br /&gt;// create customer object&lt;br /&gt;&lt;br /&gt;csd.saveOrUpdate(customer);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note, you're still free to use hibernate session if you like, all you have to do is access the hibernate session factory bean: &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;context.getBean("mySessionFactoryBean", SessionFactory.class);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Adiós!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6033871083265669572-3398296752128201854?l=tuliodomingos.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tuliodomingos.blogspot.com/feeds/3398296752128201854/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tuliodomingos.blogspot.com/2011/05/hibernate-session-management-in-spring.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/3398296752128201854'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/3398296752128201854'/><link rel='alternate' type='text/html' href='http://tuliodomingos.blogspot.com/2011/05/hibernate-session-management-in-spring.html' title='Hibernate Session Management in Spring'/><author><name>Tulio Domingos</name><uri>http://www.blogger.com/profile/14981659811312844527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://2.bp.blogspot.com/_nI0cE8KhCUs/S3arXSrk_bI/AAAAAAAAAAs/C7EZXDm6obc/S220/profile-glenfinnan.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6033871083265669572.post-6689500531749298831</id><published>2010-03-07T14:46:00.140Z</published><updated>2011-10-02T17:08:52.139+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JAX-RS support for JAXB'/><category scheme='http://www.blogger.com/atom/ns#' term='REST'/><category scheme='http://www.blogger.com/atom/ns#' term='Web Services'/><category scheme='http://www.blogger.com/atom/ns#' term='Jersey'/><category scheme='http://www.blogger.com/atom/ns#' term='JAX-RS'/><category scheme='http://www.blogger.com/atom/ns#' term='appending bytes to JSON'/><category scheme='http://www.blogger.com/atom/ns#' term='JAX-RS support for JSON'/><title type='text'>RESTful Web Services with Jersey</title><content type='html'>I've been applying Jersey more and more often in projects at work so I thought this was a good time to write a tutorial on it..&lt;br /&gt;&lt;br /&gt;Recently I posted a couple of &lt;a href="http://tuliodomingos.blogspot.com/2010/02/soap-web-services-made-easy-with-axis2.html"&gt;articles on SOAP web services&lt;/a&gt;. Here, I discuss the Representational State Transfer (REST) architecture as an alternative to SOAP and WSDL. REST has gained widespread acceptance across the web as being an easier-to-use, resource-oriented model to expose your services.&lt;br /&gt;&lt;br /&gt;Although there is room for overlap, SOAP is mostly useful for invoking behavior (as you might have already noticed on one of my &lt;a href="http://tuliodomingos.blogspot.com/2010/02/soap-web-services-made-easy-with-axis2.html"&gt;previous posts&lt;/a&gt;) while REST is good for managing information. I will not engage in a SOAP vs REST discussion here (Its way beyond the scope of this article), instead I will go through the basics of creating a REST web service API using SUN's JAX-RS implementation for Java: Jersey. If you feel like doing some reading on this topic, I recommend the article on &lt;a href="http://blogs.oracle.com/SOAandEDA/2009/04/soap_or_rest_its_about_your_pr.html"&gt;SOAP or REST? it's about your priorities!&lt;/a&gt; and also &lt;a href="http://www.devx.com/DevX/Article/8155"&gt; Giving SOAP a REST&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;First of all, lets all be clear that Jersey is not the only JAX-RS implementation out there. Other implementations by different vendors include: RESTEasy (from JBoss), CXF (from Apache), Restlet, etc. You can find a detailed discussion on this topic at the &lt;a href="http://www.infoq.com/news/2008/10/jaxrs-comparison"&gt;SOA Community Blog&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;1. Background (in a nutshell): &lt;/span&gt;&lt;br /&gt;JAX-RS is an annotation-based API for implementing RESTful web services. Jersey is the JAX-RS reference implementation from Sun. Because REST uses HTTP as the communication protocol, the REST style is constrained to a stateless client/server architecture. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2. Requirements: &lt;/span&gt;&lt;br /&gt;• Java 5 or above (because of its heavy reliance on annotations).&lt;br /&gt;• Jersey JARs. Download Jersey's archives from the &lt;a href="https://jersey.dev.java.net/"&gt;Jersey Project site&lt;/a&gt; and add them to your web project library. Here, I'm using &lt;code style="font-size:90%"&gt;asm-3.1.jar, jersey-core.jar, jersey-server.jar, jsr-311-api-1.0.jar&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3. Environment Set-Up: &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Declare Jersey's framework Servlet inside the &lt;code style="font-size:90%"&gt;web.xml&lt;/code&gt;&lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;&amp;lt;servlet&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;servlet-name&amp;gt;JerseyController&amp;lt;/servlet-name&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;servlet-class&amp;gt; &lt;br /&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;/servlet-class&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt; &lt;br /&gt;&amp;lt;/servlet&amp;gt; &lt;br /&gt;&amp;lt;servlet-mapping&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;servlet-name&amp;gt;JerseyController&amp;lt;/servlet-name &amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt; &lt;br /&gt;&amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Jersey uses a Servlet called Grizzly Servlet, and the servlet &lt;code style="font-size:90%"&gt;(com.sun.jersey.spi.container.servlet.ServletContainer)&lt;/code&gt; handles the requests to Grizzly. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;4. Implementation: &lt;/span&gt;&lt;br /&gt;In its most simplest form, Jersey requires no more than a couple of annotations to build a trivial service: &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;import javax.ws.rs.GET;&lt;br /&gt;import javax.ws.rs.Path;&lt;br /&gt;import javax.ws.rs.Produces;&lt;br /&gt;&lt;br /&gt;@Path ("/printname")&lt;br /&gt;public class PrintNameResource {&lt;br /&gt;&lt;br /&gt;&amp;ensp;@GET&lt;br /&gt;&amp;ensp;@Produces ("text/plain")&lt;br /&gt;&amp;ensp;public String printName() {&lt;br /&gt;&amp;ensp;&amp;ensp;return "Hi Tulio Domingos";&lt;br /&gt;&amp;ensp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;b&gt;@Path&lt;/b&gt; &lt;br /&gt;Indicates what URI path is mapped to the resource. For example, you would access this class using the following URL: &lt;i&gt;http://localhost:8080/&amp;lt;context&amp;gt;/printname&lt;/i&gt;&lt;br /&gt;&lt;b&gt;@GET&lt;/b&gt;&lt;br /&gt;JAX-RS provides a clear mapping between the HTTP protocol and the URIs through well-defined classes and interfaces. @GET, @POST, @DELETE, etc.&lt;br /&gt;The type of HTTP method request dictates which request method designator is called. For example, if the request is from a HTTP GET request (above), the service automatically invokes a method annotated with @GET and provides the response. &lt;br /&gt;&lt;b&gt;@Produces&lt;/b&gt; &lt;br /&gt;This annotation specifies the MIME type for the response (a representation that can be produced by a resource and sent back to the client) &lt;br /&gt;&lt;br /&gt;Above, I illustrate the use of 3 basic JAX-RS annotations. In case you feel the need to have two or more methods handling HTTP GET requests, then make sure you provide a path (@Path annotation) for each method so they don't clash. &lt;br /&gt;&lt;br /&gt;The above sample code, as you might have guessed, is useless as I have hard coded the response to client. Consider a scenario where the server constructs the response based on a URI query parameter 'name'. &lt;br /&gt;i.e. http://localhost:8080/&amp;lt;context&amp;gt;/printname?name=tulio    &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;import javax.ws.rs.GET;&lt;br /&gt;import javax.ws.rs.Path;&lt;br /&gt;import javax.ws.rs.Produces;&lt;br /&gt;import javax.ws.rs.QueryParam;&lt;br /&gt;&lt;br /&gt;@Path ("/printname")&lt;br /&gt;public class PrintNameResource {&lt;br /&gt;&lt;br /&gt;&amp;ensp;@GET&lt;br /&gt;&amp;ensp;@Produces ("text/plain")&lt;br /&gt;&amp;ensp;public String printName(@QueryParam("name") String name) {&lt;br /&gt;&amp;ensp;&amp;ensp;return "Hi " + name;&lt;br /&gt;&amp;ensp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Alternatively, you can make use of relative URI paths. As discussed earlier, you identify a root resource class using the @Path annotation. The value of the annotation can have a relative URI path template between curly braces {,}, with the deployment context providing the reference to the base URI. Note as well you use it in conjunction with the @PathParam as opposed to the @QueryParam (above)In this case, the URI would look more like:  &lt;br /&gt;i.e. http://localhost:8080/&amp;lt;context&amp;gt;/printname/tulio  &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;import javax.ws.rs.GET;&lt;br /&gt;import javax.ws.rs.Path;&lt;br /&gt;import javax.ws.rs.Produces;&lt;br /&gt;&lt;br /&gt;@Path ("/printname/{name}")&lt;br /&gt;public class PrintNameResource {&lt;br /&gt;&lt;br /&gt;&amp;ensp;@GET&lt;br /&gt;&amp;ensp;@Produces ("text/plain")&lt;br /&gt;&amp;ensp;public String printName(@PathParam("name") String name) {&lt;br /&gt;&amp;ensp;&amp;ensp;return "Hi " + name;&lt;br /&gt;&amp;ensp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Change the @Produces annotations to specify a MIME type for the response that suits your needs (HTML, XML, JSON, Image, etc). Resource classes can produce or consume any type of MIME. Use annotations to specify the MIME media type for both request and response on a resource class or resource method.&lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;import javax.ws.rs.GET;&lt;br /&gt;import javax.ws.rs.Path;&lt;br /&gt;import javax.ws.rs.Produces;&lt;br /&gt;import javax.ws.rs.QueryParam;&lt;br /&gt;&lt;br /&gt;@Path ("/printname")&lt;br /&gt;public class PrintNameResource {&lt;br /&gt;&lt;br /&gt;&amp;ensp;@GET&lt;br /&gt;&amp;ensp;@Produces("text/xml")&lt;br /&gt;&amp;ensp;public String printName(@QueryParam("name") String name) {&lt;br /&gt;&amp;ensp;&amp;ensp; return "&amp;lt;msg&amp;gt;Hi " + name + "&amp;lt;/msg&amp;gt;";&lt;br /&gt;&amp;ensp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;One of the coolest features of JAX-RS/Jersey is the automatic creation of XML and JSON via JAXB annotations! That's right, no further coding is required, below is an example of how easily a POJO can be turned into a REST service, simply by adding a JAXB annotation: &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;import javax.ws.rs.GET;&lt;br /&gt;import javax.ws.rs.Path;&lt;br /&gt;import javax.ws.rs.Produces;&lt;br /&gt;import javax.ws.rs.QueryParam;&lt;br /&gt;&lt;br /&gt;@Path ("/message")&lt;br /&gt;public class PrintNameResource {&lt;br /&gt;&amp;ensp;@GET&lt;br /&gt;&amp;ensp;@Produces("text/xml")&lt;br /&gt;&amp;ensp;public Message getMessage(@QueryParam("name") String name) {&lt;br /&gt;&amp;ensp;&amp;ensp; Message message = new Message();&lt;br /&gt;&amp;ensp;&amp;ensp; message.setContent("Hi " + name);&lt;br /&gt;&amp;ensp;&amp;ensp; return message;&lt;br /&gt;&amp;ensp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;And the message bean would look something like: &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;@XmlRootElement&lt;br /&gt;public class Message{    &lt;br /&gt;&amp;ensp;&amp;ensp;private String content; &lt;br /&gt;&amp;ensp;&amp;ensp;public String getContent(){&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;reuturn content;  &lt;br /&gt;&amp;ensp;&amp;ensp;}&lt;br /&gt;&amp;ensp;&amp;ensp;public void setContent(String c){&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;this.content = c;  &lt;br /&gt;&amp;ensp;&amp;ensp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;JAXB is often associated with web services. With JAXB you can generate XML from either an XSD or your own POJO, also you're free to manually marshal them may you find it more convenient. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;5. HttpSessions in Jersey: &lt;/span&gt;&lt;br /&gt;Feel the need to access the session object in Jersey? Below is an example on just how you can achieve this: &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;@GET&lt;br /&gt;@Path("/getsession") &lt;br /&gt;@Produces(MediaType.APPLICATION_JSON)&lt;br /&gt;@Consumes("application/x-www-form-urlencoded")&lt;br /&gt;public String getSession(@Context HttpServletRequest req){&lt;br /&gt;&amp;ensp;&amp;ensp;String jsessionid = req.getSession().getId();&lt;br /&gt;&amp;ensp;&amp;ensp;logger.info("JSESSIONID is: " + jsessionid);&lt;br /&gt;&amp;ensp;&amp;ensp;return jsessionid;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Alternatively, you can annotate your resource class with the &lt;code style="font-size:90%"&gt;@PerSession&lt;/code&gt; annotation so that a new instance of the resource class gets created for each session. This instance will remain servicing the client for as long as the session lives. &lt;br /&gt;&lt;br /&gt;Refer to its &lt;a href="https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/spi/container/servlet/PerSession.html"&gt;javadoc&lt;/a&gt; for more information.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;6. Jersey's support for JSON: &lt;/span&gt;&lt;br /&gt;Jersey added support for JSON by including the library &lt;code style="font-size:90%"&gt;jettison-1.0.jar&lt;/code&gt; in its bundle. Add this library to the build path of your project and change the &lt;b&gt;@Produces&lt;/b&gt; annotation to something like &lt;b&gt;@Produces("application/json")&lt;/b&gt;. Make sure to use it in conjunction with JAXB annotation (above). &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Note:&lt;/b&gt; If you need to append bytes to the response, whether its in JSON, XML, or any other text format, then use some form of encoding such as BASE64. Byte values would turn into illegal characters when converted to string. The same happens in a SOAP message.&lt;br /&gt;According to the &lt;a href="http://www.json.org"&gt;JSON documentation&lt;/a&gt;, JSON supports any UNICODE character except (1) control characters, (2) \ and (3) "&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;7. Jersey's support for Spring: &lt;/span&gt;&lt;br /&gt;Follow the steps below in order to integrate Spring with Jersey: &lt;br /&gt;&lt;br /&gt;• Download and add the Spring JARs to the build path of your project&lt;br /&gt;&lt;br /&gt;• Include Jersey's Spring library: &lt;code style="font-size:90%"&gt;jersey-spring.jar&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;• Also add &lt;code style="font-size:90%"&gt;antlr&lt;/code&gt; version 3: &lt;code style="font-size:90%"&gt;antlr-3.0.jar&lt;/code&gt;. This is important! If you have the wrong &lt;code style="font-size:90%"&gt;antlr&lt;/code&gt; library, you might face errors of type: &lt;code style="font-size:90%"&gt;Context initialization failed. java.lang.NoSuchFieldError: ruleMemo&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;• Configure Jersey's Spring servlet in your &lt;code style="font-size:90%"&gt;web.xml&lt;/code&gt; along with its respective init-parameters: &lt;code style="font-size:90%"&gt;com.sun.jersey.config.property.resourceConfigClass&lt;/code&gt; and &lt;code style="font-size:90%"&gt;com.sun.jersey.api.core.PackagesResourceConfig&lt;/code&gt;&lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;&amp;lt;servlet&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;servlet-name&amp;gt;Jersey Spring&amp;lt;/servlet-name&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;servlet-class&amp;gt; &lt;br /&gt;com.sun.jersey.spi.spring.container.servlet.SpringServlet&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;/servlet-class&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;init-param&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;param-name&amp;gt;&lt;br /&gt;com.sun.jersey.config.property.resourceConfigClass&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;/param-name&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;param-value&amp;gt;&lt;br /&gt;com.sun.jersey.api.core.PackagesResourceConfig&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;/param-value&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;/init-param&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;init-param&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;param-name&amp;gt;&lt;br /&gt;com.sun.jersey.config.property.packages&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;/param-name&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;param-value&amp;gt;&lt;br /&gt;com.tcm.appstore.ws.resources&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;/param-value&amp;gt;&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;lt;/init-param&amp;gt;&lt;br /&gt;&amp;lt;/servlet&amp;gt; &lt;br /&gt;&amp;lt;servlet-mapping&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;servlet-name&amp;gt;Jersey Spring&amp;lt;/servlet-name&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt; &lt;br /&gt;&amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;• Create the Spring application context file (&lt;code style="font-size:90%"&gt;spring-context.xml&lt;/code&gt;) as you would normally in Spring and specify it in the &lt;code style="font-size:90%"&gt;web.xml&lt;/code&gt; via a context parameter: &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;&amp;lt;context-param&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;param-name&amp;gt;contextConfigLocation&amp;lt;/param-name&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;param-value&amp;gt;/WEB-INF/spring-context.xml&amp;lt;/param-value&amp;gt; &lt;br /&gt;&amp;lt;/context-param&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;• Last but not least, register the Spring context loader listener also in the &lt;code style="font-size:90%"&gt;web.xml&lt;/code&gt;: &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;&amp;lt;listener&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;listener-class&amp;gt;&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;org.springframework.web.context.ContextLoaderListener&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;lt;/listener-class&amp;gt; &lt;br /&gt;&amp;lt;/listener&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;• An interesting feature of Jersey Spring, is its dependency injection mechanism through the use of annotations. This feature enables the injection of a Spring bean into you application using the &lt;code style="font-size:90%"&gt;@Inject&lt;/code&gt; annotation. Below is an example of such feature (assume the message bean is declared in the &lt;code style="font-size:90%"&gt;spring-context.xml&lt;/code&gt;): &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;import javax.ws.rs.GET;&lt;br /&gt;import javax.ws.rs.Path;&lt;br /&gt;import javax.ws.rs.Produces;&lt;br /&gt;import com.sun.jersey.spi.inject.Inject;&lt;br /&gt;&lt;br /&gt;@Path ("/message")&lt;br /&gt;public class PrintNameResource {&lt;br /&gt;&lt;br /&gt;&amp;ensp;@Inject&lt;br /&gt;&amp;ensp;Message message;&lt;br /&gt;&lt;br /&gt;&amp;ensp;@GET&lt;br /&gt;&amp;ensp;@Produces("text/xml")&lt;br /&gt;&amp;ensp;public Message getMessage() {   &lt;br /&gt;&amp;ensp;&amp;ensp;return message;&lt;br /&gt;&amp;ensp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Please bear in mind that all code samples outlined in this article were &lt;b&gt;not tested&lt;/b&gt;, as a result, syntax mistakes might have occurred at some point down the line. Any form of feedback is welcome. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;6. Further Reading: &lt;/span&gt;&lt;br /&gt;• &lt;a href="http://www.javaworld.com/javaworld/jw-10-2008/jw-10-rest-series-1.html"&gt;http://www.javaworld.com/javaworld/jw-10-2008/jw-10-rest-series-1.html&lt;/a&gt;&lt;br /&gt;• &lt;a href="http://www.devx.com/Java/Article/42873/1954"&gt;http://www.devx.com/Java/Article/42873/1954&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6033871083265669572-6689500531749298831?l=tuliodomingos.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tuliodomingos.blogspot.com/feeds/6689500531749298831/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tuliodomingos.blogspot.com/2010/03/restful-web-services-with-jersey.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/6689500531749298831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/6689500531749298831'/><link rel='alternate' type='text/html' href='http://tuliodomingos.blogspot.com/2010/03/restful-web-services-with-jersey.html' title='RESTful Web Services with Jersey'/><author><name>Tulio Domingos</name><uri>http://www.blogger.com/profile/14981659811312844527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://2.bp.blogspot.com/_nI0cE8KhCUs/S3arXSrk_bI/AAAAAAAAAAs/C7EZXDm6obc/S220/profile-glenfinnan.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6033871083265669572.post-5094930436588456581</id><published>2010-02-21T13:39:00.020Z</published><updated>2010-02-22T12:27:25.866Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='Axis2 example'/><category scheme='http://www.blogger.com/atom/ns#' term='Axis2 tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='SOAP'/><category scheme='http://www.blogger.com/atom/ns#' term='Axis2 Client'/><category scheme='http://www.blogger.com/atom/ns#' term='Axis'/><category scheme='http://www.blogger.com/atom/ns#' term='SOAP client'/><category scheme='http://www.blogger.com/atom/ns#' term='Apache SOAP Framework'/><title type='text'>BCLService - Accessing the web service with a generated client</title><content type='html'>On my &lt;a href="http://tuliodomingos.blogspot.com/2010/02/soap-web-services-made-easy-with-axis2.html"&gt;previous post&lt;/a&gt;, I explained how you can expose your existing java API as a SOAP web sevice using Apache Axis2. I went through the steps of creating a web service called "BCLService"...Now here's the sequel, we are going to look into how we can interact with the service using a generated client. &lt;br /&gt;&lt;br /&gt;This is a step by step guide to interacting with the BCLService using a client based on generated code.  This guide uses Axis 2 as an implementation of the SOAP protocol. Axis is a framework for constructing SOAP clients and servers. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;1. Requirements: &lt;/span&gt;&lt;br /&gt;&lt;blockquote&gt;JDK 1.5 (or latest)&lt;br /&gt;Axis 2 1.4 (or latest)&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;You can download the latest release of Axis2 from the &lt;a href="http://ws.apache.org/axis2/download/1_4/download.cgi"&gt;Apache download site&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Apache provides a tool in the Axis2 distribution (WSDL2Java) to generate client side stubs that interacts with the service (You can find WSD2Java in "org.apache.axis.wsdl.WSDL2Java"). &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2. Generating client stubs using the Apache WSDL2Java tool: &lt;/span&gt;Client programs need only to know the EPR and the operation name (both listed on the WSDL) in order to interact with your web service. &lt;br /&gt;&lt;br /&gt;In order to generate the stubs, run the following command:&lt;br /&gt;&lt;br /&gt;&lt;code style="font-size:90%"&gt;•[Linux]&lt;br /&gt;$ sh WSDL2Java.sh -uri http://&lt;host&gt;:&lt;port&gt;/axis2/services/&lt;your_service&gt;?wsdl -o /path/to/your/client/code/&lt;br /&gt;•[Windows]&lt;br /&gt;$ WSDL2Java.bat -uri http://&lt;host&gt;:&lt;port&gt;/axis2/services/&lt;your_service&gt;?wsdl -o /path/to/your/client/code/&lt;/code&gt;&lt;blockquote style="font-size:85%"&gt;&lt;span style="font-style:italic;"&gt;&lt;span style="font-weight:bold;"&gt;-uri &lt;/span&gt;Specifies the location of the WSDL file. In other terms, the Endpoint Reference (EPR) of the service with the “?wsdl” argument appended in the end.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;-o &lt;/span&gt;Specifies the output path for the tool, in other words, the location where you want WSD2Java to generate the client code, Note that you need not to specify the source folder (/src) in the parameter as the source folder will already be added by WSDL2Java &lt;br /&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;br /&gt;The above command generates 2 java classes. Now, all you need to do is to create a simple class (i.e. Client.java) which uses the stub to call the service. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3. Calling the Web Service: &lt;/span&gt;The following is a sample client class code used to interact with the web service. &lt;br /&gt;Make sure to add all the Axis JARs that the stubs refer to (JARs can be found under &lt;code style="font-size:80%"&gt;AXIS_HOME/axis2-1.4/lib&lt;/code&gt;).&lt;br /&gt;&lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;public class Client {    &lt;br /&gt;&amp;ensp;&amp;ensp;public static void main(String[] args) &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp; throws RemoteException {&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;MyServiceStub stub = new MyServiceStub(); &lt;br /&gt;&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;/* Invoking the GetCurrentPosition service */&lt;br /&gt;&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;//Create the request &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;MyServiceStub.GetCurrentPosition request = &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp; new MyServiceStub.GetCurrentPosition (); &lt;br /&gt;&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;/* if the method doesn’t take parameters, then just &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp; skip the request object and instantiate the reponse &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp; object directly by calling the method on the stub */ &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;request.setClubName("flamengo");&lt;br /&gt;&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;GetCurrentPosition Response response = &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;  stub.GetCurrentPosition(request);&lt;br /&gt;&lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;System.out.println("Web Service returned : " + &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;response.get_return());&lt;br /&gt;&amp;ensp;&amp;ensp;}    &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Now you can compile and run the client code. Its is important to note that the classpath must have all the jars in the "lib" directory of the axis2 distribution when compiling and running the client.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6033871083265669572-5094930436588456581?l=tuliodomingos.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tuliodomingos.blogspot.com/feeds/5094930436588456581/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tuliodomingos.blogspot.com/2010/02/bclservice-accessing-web-service-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/5094930436588456581'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/5094930436588456581'/><link rel='alternate' type='text/html' href='http://tuliodomingos.blogspot.com/2010/02/bclservice-accessing-web-service-with.html' title='BCLService - Accessing the web service with a generated client'/><author><name>Tulio Domingos</name><uri>http://www.blogger.com/profile/14981659811312844527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://2.bp.blogspot.com/_nI0cE8KhCUs/S3arXSrk_bI/AAAAAAAAAAs/C7EZXDm6obc/S220/profile-glenfinnan.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6033871083265669572.post-7299234982074568131</id><published>2010-02-13T13:58:00.015Z</published><updated>2011-10-02T17:21:40.695+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tulio'/><category scheme='http://www.blogger.com/atom/ns#' term='Axis2 example'/><category scheme='http://www.blogger.com/atom/ns#' term='Axis2 tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='Axis'/><category scheme='http://www.blogger.com/atom/ns#' term='Apache SOAP Framework'/><title type='text'>SOAP  Web Services made easy with Axis2</title><content type='html'>Ever felt the need of having a quick SOAP Web Service without getting your hands dirty with SOAP implementations? &lt;br /&gt;&lt;br /&gt;Well, Apache Axis2 is probably your best bet! Its framework provides utilities for generating and deploying web service client and servers. I'm not going to get into the details of its architecture or its improvements compared to earlier versions of Axis. You can visit &lt;a href="http://ws.apache.org/axis2/index.html"&gt; Axis2 home page&lt;/a&gt; for a detailed description. &lt;br /&gt;&lt;br /&gt;This tutorial takes you through the necessary steps required to expose your existing classes as a web service using Axis2 for Java. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Requirements: &lt;/span&gt; First download Axis2 from the &lt;a href="http://ws.apache.org/axis2/download.cgi"&gt;Apache website&lt;/a&gt; and install it according to the "installation.txt" found inside the package. &lt;br /&gt;&lt;br /&gt;Set AXIS2_HOME to your environment variables and also "%AXIS2_HOME%/bin" (include the bin directory) to your PATH&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Scenario: &lt;/span&gt; You have a simple bean that provides a service to a client application (i.e. The client gives the name of a football team in the Brazilian cup league and the server responds with the team's current position in the league) This is done by invoking the "getCurrentPosition(String team)" of the class "BCLService.java"&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Exposing your class as a web service: &lt;/span&gt; Actually, what you expose are the methods of the class and not the class itself. You can expose the "getCurrentPosition(String team)" method to the client by simply making it available in a configuration file.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;1. Configuration: &lt;/span&gt; Create a simple XML file "service.xml" which will inform Axis2 about its services. The following is a sample "service.xml" for our "BCLService" class: &lt;br /&gt;&lt;code style="font-size:90%"&gt;&lt;br /&gt;&amp;lt;service&amp;gt; &lt;br /&gt;&amp;ensp; &amp;lt;parameter name="ServiceClass" locked="false"&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp; com.xyz.BCLService &lt;br /&gt;&amp;ensp; &amp;lt;/parameter&amp;gt; &lt;br /&gt;&amp;ensp; &amp;lt;operation name="getCurrentPostion"&amp;gt; &lt;br /&gt;&amp;ensp;&amp;ensp;&amp;ensp; &amp;lt;messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /&amp;gt; &lt;br /&gt;&amp;ensp; &amp;lt;/operation&amp;gt; &lt;br /&gt;&amp;lt;/service&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;blockquote style="font-size:85%"&gt;&lt;span style="font-weight:bold;"&gt;Note:&lt;/span&gt; &lt;span style="font-style:italic;"&gt;The value of the xml element &amp;lt;parameter&amp;gt; must be the full qualified name your java class.&lt;br /&gt;All public methods of your declared class will be exposed by default. If you wish not to expose a method, then use the &amp;lt;excludeOperation&amp;gt; element.&lt;br /&gt;The "locked" attribute of element &amp;lt;parameter&amp;gt; is set to false so that any child node parameter can override it.&lt;br /&gt;The "name" attribute of element &amp;lt;operation&amp;gt; must be the name of the method you wish to expose in the web service (you can add as many as you want, however, Axis2 does not support overloaded methods! Operation names must be unique!)&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2. Packaging: &lt;/span&gt;Axis2 expects its services to be packaged according to a specific format: &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2.1 &lt;/span&gt; Create a "tmp" dir &lt;span style="font-style:italic;"&gt;(just for packaging purpose)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2.2 &lt;/span&gt; Compile your "BCLService.java" class (&lt;code style="font-size:80%"&gt;javac BCLService.java&lt;/code&gt;) and move it to the "tmp" directory&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2.3 &lt;/span&gt; Create a "META-INF" directory inside the "tmp" directory and place the "service.xml" config file inside it.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2.4 &lt;/span&gt; Finally, in order to package everything into an axis archive (aar), run the following command from the "tmp" dir level:&lt;br /&gt;&lt;br /&gt;&lt;code style="font-size:90%"&gt; Jar cvf BCLService.aar . &lt;/code&gt; &lt;br /&gt;&lt;blockquote style="font-size:85%"&gt;&lt;span style="font-style:italic;"&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Jar &lt;/span&gt; The jar command generates an archive file in the same directory &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;cvf &lt;/span&gt; The cvf option indicates that you want to create a JAR file in verbose mode and that the output should go to a file rather than to stdout. For more information on creating jar files, visit &lt;a  style="font-size:80%"href="http://java.sun.com/docs/books/tutorial/deployment/jar/build.html"&gt; SUN's Tutorial "Packaging Programs in JAR"&lt;/a&gt; &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;BCLService.aar &lt;/span&gt; Is actually a jar, just renamed to "aar" (axis archive) to distinguish from other jars – note that this is optional&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;. &lt;/span&gt; Package everything in the current directory &lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3. Hosting the Service: &lt;/span&gt;Below are the 2 most popular ways of hosting the service: &lt;br /&gt;&lt;br /&gt;* Using the Simple HTTP Server  that comes in Axis2 distribution &lt;br /&gt;* Use Axis2 to generate a WAR file and deploy it in a servlet container of your choice. (i.e. Tomcat)&lt;br /&gt;&lt;br /&gt;The following steps will take you through the 2nd option (create WAR from AAR):&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3.1 &lt;/span&gt;Place your newly created "BCLService.aar" in the following directory: &lt;br /&gt;&lt;code style="font-size:80%"&gt;%AXIS2_HOME%/repository/services&lt;/code&gt; &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3.2 &lt;/span&gt;Execute target "create.war" from the ant build file at: &lt;br /&gt;&lt;code style="font-size:80%"&gt;%AXIS2_HOME%/webapps/build.xml&lt;/code&gt; &lt;br /&gt;&lt;span style="font-style:italic;"&gt;i.e. run command: &lt;code style="font-size:80%"&gt;ant create.war&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3.3 &lt;/span&gt;Find the generated "axis2.war" inside: &lt;br /&gt;&lt;code style="font-size:80%"&gt;%AXIS2_HOME%/dist&lt;/code&gt; &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3.4 &lt;/span&gt;If you’re using Tomcat as your servlet container, drop the WAR file in the "webapps" dir of Tomcat, then start the server.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;4. Running the web service application: &lt;/span&gt;To verify the deployment go to: &lt;span style="font-style:italic;font-size:70%"&gt;http://&amp;lt;HOST&amp;gt;:&amp;lt;PORT&amp;gt;/axis2/&lt;/span&gt;&lt;br /&gt;Follow the "services" link and check that the "getCurrentPosition(String name)" operation is available from the list of operations.&lt;br /&gt;&lt;br /&gt;It will also list the endpoint reference URL (EPR) but you will not be able to access it from the browser since Axis2 doesn’t expect input from the browser.&lt;br /&gt;&lt;br /&gt;Click on the "BCLService" service link to view its WSDL file. Clients can now simply look up your WSDL to find out what operations your service exposes in order to call them. &lt;br /&gt;&lt;br /&gt;Note that Axis2 generated the WSDL file for you, however, you are also free to create one or your own, just drop your own WSDL file (i.e. BCLService.wsdl) in the META-INF directory of the Axis Archive (AAR).&lt;br /&gt;&lt;br /&gt;Check out my post on &lt;a href="http://tuliodomingos.blogspot.com/2010/02/bclservice-accessing-web-service-with.html"&gt;BCLService - Accessing the web service with a generated client&lt;/a&gt; for steps on how you can interact with the web service.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6033871083265669572-7299234982074568131?l=tuliodomingos.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tuliodomingos.blogspot.com/feeds/7299234982074568131/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tuliodomingos.blogspot.com/2010/02/soap-web-services-made-easy-with-axis2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/7299234982074568131'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6033871083265669572/posts/default/7299234982074568131'/><link rel='alternate' type='text/html' href='http://tuliodomingos.blogspot.com/2010/02/soap-web-services-made-easy-with-axis2.html' title='SOAP  Web Services made easy with Axis2'/><author><name>Tulio Domingos</name><uri>http://www.blogger.com/profile/14981659811312844527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://2.bp.blogspot.com/_nI0cE8KhCUs/S3arXSrk_bI/AAAAAAAAAAs/C7EZXDm6obc/S220/profile-glenfinnan.jpg'/></author><thr:total>0</thr:total></entry></feed>
