Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Tuesday, April 8, 2014

How to download spring framework zip file

Unfortunately there is no official link on the new spring.io website. You now have to download the zip files form their repository, as stated here: https://github.com/spring-projects/spring-framework/wiki/Downloading-Spring-artifacts (at the bottom).
Direct link to complete Spring zip files: http://repo.spring.io/release/org/springframework/spring/
 
 
Download spring-framework-3.2.4 here.
http://maven.springframework.org/release/org/springframework/spring/3.2.4.RELEASE/ 


It is always recommended to use Maven like build tools as it fetches not only the Spring JARs but also the JARs which Spring depends upon (like e.g. commons-logging) and so on... How to do this quickly is given in this post by "fujy":- Where can I download Spring Framework jars
Cheers, Akshay
 
 
 
You can find out all spring jar in zip format at here

can i use Spring framework for developing JSP application?

You can use Spring MVC. Try following tutorials:
http://static.springsource.org/docs/Spring-MVC-step-by-step/
http://www.vaannila.com/spring/spring-mvc-tutorial-1.html
http://maestric.com/doc/java/spring
Thanks.

Wednesday, January 22, 2014

Install PostgreSQL 9.3 & psycopg2 on CentOS 6.4

Install yum in the PostgreSQL 9.3 on CentOS 6.4, is a Python binding psycopg2 note of procedures for installing the pip.

Installation of PostgreSQL

I use the RPM that the developer of PostgreSQL provides

Configure your YUM repository

Modification of the YUM repository
I will be out of the jurisdiction from PostgreSQL YUM repository of default
/etc/yum.repos.d/CentOS-Base.repo Add the following to the section of the [updates] and [base]
exclude=postgresql*

Install PostgreSQL Global Development Group of (PGDG)

By installing the PGDG, RPM packages of the following are available for installation
  • postgresql-libs:. The postgresql-libs package provides the essential shared libraries for any PostgreSQL client program or interface You will need to install this package to use any other PostgreSQL package or any clients that need to connect to a PostgreSQL server.
  • postgresql:. ​​If you want to manipulate a PostgreSQL database on a local or remote PostgreSQL server, you need this package You also need to install this package if you're installing the postgresql-server package.
  • postgresql-contrib: The postgresql-contrib package contains contributed packages that are included in the PostgreSQL distribution.
  • postgresql-devel:. The postgresql-devel package contains the header files and libraries needed to compile C or C + + applications which will directly interact with a PostgreSQL database management server and the ecpg Embedded C Postgres preprocessor You need to install this package if you want to develop applications which will interact with a PostgreSQL server.
  • postgresql-docs:. The postgresql-docs package includes the SGML source for the documentation as well as the documentation in PDF format and some extra documentation Install this package if you want to help with the PostgreSQL documentation project, or if you want to generate printed documentation.
  • postgresql-server:. The postgresql-server package includes the programs needed to create and run a PostgreSQL server, which will in turn allow you to create and maintain PostgreSQL databases You should install postgresql-server if you want to create and maintain your own PostgreSQL databases and / or your own PostgreSQL server. You also need to install the postgresql package and its requirements.
  • postgresql-tcl: The postgresql-tcl package contains the Pgtcl client library and its documentation.
  • postgresql-jdbc:. The postgresql-jdbc package includes the jar files needed for Java programs to access a PostgreSQL database.
  • postgresql-pl:. The postgresql-pl package contains the the PL / Perl, and PL / Python procedural languages ​​for the backend PL / Pgsql is part of the core server package.
  • postgresql-python: The postgresql-python package includes a module for developers to use when writing Python code for accessing a PostgreSQL database.
  • postgresql-test: The postgresql-test package includes the sources and pre-built binaries of various tests for the PostgreSQL database management system, including regression tests and benchmarks.
PGDG are present in the version / distribution / architecture each of PostgreSQL, this is to download and install what they want from the following pages.
The following assumed that you are installing on x86_64 of the 6 CentOS PostgreSQL9.3.
$ curl -O http://yum.postgresql.org/9.3/redhat/rhel-6.4-x86_64/pgdg-centos93-9.3-1.noarch.rpm
$ sudo rpm -ivh pgdg-centos93-9.3-1.noarch.rpm
warning: pgdg-centos93-9.3-1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                ########################################### [100%]
   1:pgdg-centos93          ########################################### [100%]

$ yum list postgres*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
base                                                                                                                       | 3.7 kB     00:00
extras                                                                                                                     | 3.4 kB     00:00
pgdg93                                                                                                                     | 2.8 kB     00:00
pgdg93/primary_db                                                                                                          |  61 kB     00:00
updates                                                                                                                    | 3.4 kB     00:00
Available Packages
postgresql93.x86_64                                                         9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-contrib.x86_64                                                 9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-debuginfo.x86_64                                               9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-devel.x86_64                                                   9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-docs.x86_64                                                    9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-libs.x86_64                                                    9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-plperl.x86_64                                                  9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-plpython.x86_64                                                9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-pltcl.x86_64                                                   9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-server.x86_64                                                  9.3.0-1PGDG.rhel6                                               pgdg93
postgresql93-test.x86_64                                                    9.3.0-1PGDG.rhel6                                               pgdg93
postgresql_autodoc.noarch                                                   1.41-1.rhel6                                                    pgdg93

$ sudo yum install postgresql93 postgresql93-contrib postgresql93-devel postgresql93-libs postgresql93-server
Program /usr/pgsql-9.3/bin/ is installed below. The PGDATA is /var/lib/pgsql/9.3/data is set to.

Launch configuration

To auto-start chkconfig Set
$ sudo /sbin/chkconfig --list postgresql-9.3
postgresql-9.3  0:off   1:off   2:off   3:off   4:off   5:off   6:off
$ sudo /sbin/chkconfig postgresql-9.3 on
$ sudo /sbin/chkconfig --list postgresql-9.3
postgresql-9.3  0:off   1:off   2:on    3:on    4:on    5:on    6:off

$ sudo service postgresql-9.3 initdb
Initializing database:                                     [  OK  ]
$ sudo service postgresql-9.3 start
Starting postgresql-9.3 service:                           [  OK  ]
(You can create a user After $ createuser --interactive -W ), pg_hba.conf You can play around with the file.

Installation of psycopg2

Is a Python bindings for PostgreSQL psycopg2 the pip install.
First installation of pip
$ wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | sudo python
$ sudo easy_install pip
psycopg2 is extensible module and C, I put the development package of Python
$ sudo yum install python-devel
The build of psycopg2 pg_config it is necessary to execute the command.
/usr/pgsql-9.3/bin because it is installed in the path of irregular, environment variable PATH to adjust.
sudo When used in a environment variable for security PATH for is not taken over , run to become root.
$ sudo su
# export PATH=/usr/pgsql-9.3/bin:$PATH
#  pip install psycopg2
...
Successfully installed psycopg2
Cleaning up...

About pg_config package

pg_config package postgresql93-devel is installed in. pg_config If you can not be found, an error message similar to the following is displayed.
Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

References

Wednesday, January 9, 2013

From Spring to Java EE 6

I recently worked on a quite complex project mixing many Java EE 6 technologies (such as JPA, JAXB, JMS, JTA, JAX-RS, etc…). For productivity and planning reasons, the prototyped application was designed as a standalone pure Spring application. When the development of the real application started, we re-challenged our initial choice (i.e. Spring v3) and analyzed the interest of switching to a Java EE 6 app server like GlassFish or JBoss.
This finally ended up in two major questions:
  • can we do in Java EE 6 everything we can do in Spring ?
  • can we do that as easy as in Spring ?
Well, I would say that, globally, the answer is: yes we can !
I do not want to reopen the (endless) debate of knowing which one, between Spring and Java EE 6, is the best. No, I just want to share with you my experience regarding this migration. I was – and I am still – a real Spring fanboy (which I, historically speaking, discovered after having been literally disgusted by EJB’s 1.0), but I am also aware of the recent progress, not to say simplifications, that have been introduced in Java EE  these last years, as well as the impressive speed improvements on the side of Java EE 6 application servers.
Let us now study in details some typical requirements of “enterprise” applications, and compare the code to produce in both worlds for:
  • Contexts & Dependency Injection
  • Messaging
  • Transaction management
  • Web services
This comparison should provide you with some concrete decision elements in the event you hesitate to migrate from one technology to the other…

Part I : Contexts & Dependency Injection (CDI)

Spring allows you to define beans using various stereotypes (eg @Repository, @Service, @Controller, and @Component). The one to choose is not that important (that’s not entirely true. For instance, tagging your DAO as @Repository will add the automatic translation of SQL exceptions) as this distinction is mostly intended to IDE’s (in order to categorize beans). Optionally, you can give your bean an alias.
1public interface MyInterface {...}
1import org.springframework.stereotype.Component;
2 
3@Component("firstBean")
4public class MySpringBean implements MyInterface {...}
5 
6@Component("firstBeanMock")
7public class MockImpl implements MyInterface {...}
Java EE provides a very similar annotation (@Named) but its use should be limited to pure pojo’s. In case of service-oriented beans (especially transactional coarse-grained services), consider using a (preferably stateless) EJB – namely because they offer better scalability.
1import javax.inject.Named;
2 
3@Named("firstBean")
4public class MyJeeBean implements MyInterface {...}
1import javax.ejb.Stateless;
2 
3@Stateless(name="firstService")
4public class MyJeeService implements MyInterface {...}
Also beware that, in contrary to Spring, singletons should be explicitly marked as such in Java EE:
1import javax.inject.Singleton;
2 
3@Singleton
4public class MyJeeSingleton implements MyInterface {...}
Remark: you may get confused when choosing between a “javax.inject.Singleton” and a “javax.ejb.Singleton”. The first one defines a standard POJO managed by the container (aka a “Managed Bean” in the Java EE world), while the second one defines an “Enterprise Bean”. Remember that the later is designed for concurrent access (a client doesn’t need to worry about any other clients that may be simultaneously invoking the same methods of the singleton) and also offers transaction management facilities (see further).
Now that we have registered (and optionally named) our beans, we can inject them in other beans. Once again, the procedure is somewhat similar at both sides:
SPRING
01import org.springframework.stereotype.Component;
02import org.springframework.beans.factory.annotation.Autowired;
03import org.springframework.beans.factory.annotation.Qualifier;
04 
05@Component
06public class UseCaseHandler {
07 
08  @Autowired
09  @Qualifier("firstBean")
10  private MyInterface serviceFacade;
11   
12}
JAVA EE 6
01import javax.inject.Named;
02import javax.inject.Inject;
03 
04@Named
05public class UseCaseHandler {
06 
07  @Inject
08  @Named("firstBean"
09  private MyInterface serviceFacade;
10   
11}
Remark: The JSR-330 has unified the way to inject managed beans. This concretely means that the @Inject annotation can be used to inject simple POJOs as well as EJB’s (making thereby the @EJB annotation a bit obsolete).
Fine ! However, in the real world, the name (eg “firstBean”) of the beans we want to inject might be dynamic. This is particularly true as soon as you play with behavioral patterns, generics, etc…
In Spring, this is pretty easy. You can for instance make your bean ApplicationContext-aware, so that you can then use the injected Spring context in order to lookup for specific bean instances:
01import org.springframework.beans.BeansException;
02import org.springframework.context.ApplicationContext;
03import org.springframework.context.ApplicationContextAware;
04import org.springframework.stereotype.Service;
05 
06import com.javacodegeeks.Request;
07 
08@Service
09public class Dispatcher implements ApplicationContextAware {
10  
11 private ApplicationContext appContext;
12 
13 public void setApplicationContext(ApplicationContext ctx) throws BeansException {
14  appContext = ctx;
15 }
16  
17 public void dispatch(Request request) throws Exception {
18  String beanName = "requestHandler_" + request.getRequestTypeId();
19  RequestHandler myHandler = appContext.getBean(beanName, RequestHandler.class);
20  myHandler.handleRequest(request);
21 }
22  
23}
1public interface RequestHandler  {
2 public void handleRequest(Request request);
3}
1@Component("requestHandler_typeA")
2public class HandlerA implements RequestHandler {...}
1@Component("requestHandler_typeB")
2public class HandlerB implements RequestHandler {...}
In Java EE 6, the same is possible but yet requires a bit more lines of code (that could be centralized in an helper class):
01import java.util.Set;
02import javax.inject.Inject;
03import javax.inject.Named;
04import javax.enterprise.context.spi.CreationalContext;
05import javax.enterprise.inject.spi.Bean;
06import javax.enterprise.inject.spi.BeanManager;
07 
08import com.javacodegeeks.Request;
09 
10@Named
11public class Dispatcher
12  
13 @Inject
14 private BeanManager beanManager;
15  
16 public void dispatch(Request request) throws Exception {
17  String beanName = "requestHandler_" + request.getRequestTypeId();
18  RequestHandler myHandler = this.getBean(beanName, RequestHandler.class);
19  myHandler.handleRequest(request);
20 }
21  
22 @SuppressWarnings("unchecked")
23 private <T> T getBean(String name, Class<T> clazz) throws Exception {
24  Set<Bean<?>> founds = beanManager.getBeans(name);
25  if ( founds.size()==0 ) {
26   throw new Exception("No such bean found: "+name);
27  } else {
28   Bean<T> bean = (Bean<T>) founds.iterator().next();
29   CreationalContext<T> cc = beanManager.createCreationalContext(bean);
30          T instance = (T) beanManager.getReference(bean, clazz, cc);
31          return instance;
32  }
33 }
34  
35}
1public interface RequestHandler  {
2 public void handleRequest(Request request);
3}
1@Named("requestHandler_typeA")
2public class HandlerA implements UseCaseHandler {…}
1@Named("requestHandler_typeB")
2public class HandlerB implements UseCaseHandler {...}

PART II : JMS

Java Messaging Service eases the implementation of a loosely coupled distributed communication.
This is why it has become a classical technique in Enterprise Application Integration (EAI).
Spring has an outstanding JMS support. You can very quickly setup JMS producers or consumers,
with destination resolvers, and optionally with an automatic conversion of JMS messages into pojos (and vice-versa). On the other hand, J2EE comes with a rich set of annotations in order to access or define JMS resources such as queue/topics, connection or messages-oriented beans.
Let’s start with a JMS client that receives messages, that is a message consumer (or subscriber):
SPRING
01<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
02 <property name="environment">
03         <props>…</props>
04 </property>
05</bean>
06     
07<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
08      <property name="jndiTemplate" ref="jndiTemplate" />
09      <property name="jndiName" value="java:/JmsXA" />
10</bean>
11     
12<bean id="jndiDestResolver"
13 class="org.springframework.jms.support.destination.JndiDestinationResolver">
14 <property name="jndiTemplate" ref="jndiTemplate" />
15</bean>
16  
17<bean id="jmsContainer"
18 class="org.springframework.jms.listener.DefaultMessageListenerContainer">
19 <property name="connectionFactory" ref="jmsConnectionFactory"/>  
20 <property name="destinationResolver" ref="jndiDestResolver"/> 
21 <property name="destinationName" value="queue/myQueue"/>
22 <property name="messageListener" ref="myMsgConsumer" />
23</bean>
24    
25<bean id="myMsgConverter" class="com.javacodegeeks.MsgToRequestConverter"/>
26    
27<bean id="myMsgConsumer" class="com.javacodegeeks.MsgConsumer"/>
01import javax.jms.Message;
02import javax.jms.MessageListener;
03import org.springframework.beans.factory.annotation.Autowired;
04import org.springframework.jms.support.converter.MessageConverter;
05 
06import com.javacodegeeks.Request;
07import com.javacodegeeks.Dispatcher;
08         
09/**
10 * Example of message consumer (Message-Driven-Pojo) in Spring
11 */
12public class MsgConsumer implements MessageListener {
13  
14    @Autowired
15    private MessageConverter msgConverter;
16  
17    @Autowired
18    private Dispatcher dispatcher;
19      
20    public void onMessage(Message message) {      
21     try {
22      Request request = (Request) msgConverter.fromMessage(message);
23      dispatcher.dispatch(request);
24     } catch (Exception e) {
25    e.printStackTrace();   
26     }
27    }
28 
29}
JAVA EE 6
01import javax.inject.Inject;
02import javax.jms.Message;
03import javax.jms.MessageListener;
04import javax.ejb.MessageDriven;
05import javax.ejb.ActivationConfigProperty;
06 
07import com.javacodegeeks.Request;
08import com.javacodegeeks.Dispatcher ;
09import com.javacodegeeks.MsgToRequestConverter;
10 
11/**
12 * Example of message consumer (Message-Driven-Bean) in JEE
13 */
14@MessageDriven(activationConfig = {
15 @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
16 @ActivationConfigProperty(propertyName="destination", propertyValue="queue/myQueue")
17} )    
18public class MsgConsumer implements MessageListener  {
19  
20 @Inject
21     private MsgToRequestConverter msgConverter;
22 
23 @Inject
24 private Dispatcher dispatcher;
25  
26 public void onMessage(Message message) {      
27     try {
28      Request request = msgConverter.fromMessage(message);
29      dispatcher.dispatch(request);     
30     } catch (Exception e) {
31  e.printStackTrace();    
32 }
33    }
34     
35}
Let’s now code a JMS client that creates and sends messages, that is a message producer (or publisher):
SPRING
01<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
02 <property name="environment">
03    <props>…</props>
04 </property>
05</bean>
06     
07<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
08      <property name="jndiTemplate" ref="jndiTemplate" />
09      <property name="jndiName" value="java:/JmsXA" />
10</bean>
11     
12<bean id="jndiDestResolver"
13 class="org.springframework.jms.support.destination.JndiDestinationResolver">
14 <property name="jndiTemplate" ref="jndiTemplate" />
15</bean>
16 
17<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
18 <property name="connectionFactory" ref="jmsConnectionFactory" />
19 <property name="destinationResolver" ref="jndiDestResolver" />
20 <property name="messageConverter" ref="myMsgConverter" />
21</bean>
22     
23<bean id="myMsgConverter" class="com.javacodegeeks.MsgConverter">
01import org.springframework.stereotype.Component;
02import org.springframework.beans.factory.annotation.Autowired;
03import org.springframework.jms.core.JmsTemplate;
04import com.javacodegeeks.Request;
05 
06/**
07 * Example of message producer component in Spring
08 */
09@Component
10public class MsgProducer {
11 
12 @Autowired
13 private JmsTemplate jmsTemplate;
14  
15 public void postRequest(Request request) throws Exception {
16  jmsTemplate.convertAndSend("queue/myQueue", request);
17 }
18  
19}
JAVA EE 6
01import javax.annotation.PostConstruct;
02import javax.annotation.PreDestroy;
03import javax.annotation.Resource;
04import javax.inject.Inject;
05import javax.jms.Connection;
06import javax.jms.ConnectionFactory;
07import javax.jms.JMSException;
08import javax.jms.Message;
09import javax.jms.MessageProducer;
10import javax.jms.Queue;
11import javax.jms.Session;
12import javax.ejb.Stateless;
13import javax.ejb.EJBException;
14 
15import com.javacodegeeks.Request;
16import com.javacodegeeks.MsgToRequestConverter;
17 
18/**
19 * Example of message producer (here a session bean) in JEE
20 */
21@Stateless(name="msgProducer")
22public class MsgProducer {
23  
24 @Inject
25     private MsgToRequestConverter msgConverter;
26 
27 @Resource(mappedName="java:/JmsXA")
28 private ConnectionFactory connectionFactory;
29  
30 @Resource(mappedName="queue/myQueue")
31 private Queue queue;
32  
33 private Connection jmsConnection;
34 
35 
36 @PostConstruct
37 private void initialize() {
38  try {
39   jmsConnection = connectionFactory.createConnection();
40  } catch (JMSException e) {
41   throw new EJBException(e);
42  }
43 }
44  
45  
46 @PreDestroy
47 private void cleanup() {
48  try {
49   if (jmsConnection!=null) jmsConnection.close();
50  } catch (JMSException e) {
51   throw new EJBException(e);
52  }
53 }
54  
55  
56 public void postRequest(Request request) throws Exception {
57  Session session = null;
58  MessageProducer producer = null;
59  try {
60   session = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
61   producer = session.createProducer(queue);
62   Message msg = msgConverter.toMessage(request, session);
63       producer.send(msg); 
64  } finally {
65   try {
66    if (producer!=null) producer.close();
67    if (session!=null) session.close();
68   } catch (Exception e) {
69    System.err.println("JMS session not properly closed: "+ e);
70   }
71  }
72 }
73  
74}
Remarks:
  • Do not forget that, in contrary to JMS connections and JMS queues, JMS sessions are not thread-safe. Sessions should therefore not be shared by all bean instances, nor be created in the constructor or in a PostConstruct method.
  • PostConstruct and PreDestroy methods should only throw runtime exceptions; this is the reason why JMS exceptions have to be wrapped (for instance) into EJB exceptions.

Part III : Transaction management

The need for transactions is crucial in system architecture, especially with the advent of SOA. In such architectures, coarse-grained transactional services can be built by assembling existing – possibly also transactional – smaller services (“microservices”).
Both Spring and Java EE fulfills this need by offering a powerful declarative (annotation-based) transaction management.
SPRING
1<!-- Recognize @Transactional annotations in our beans -->
2<tx:annotation-driven transaction-manager="txManager"/>
3 
4<!-- The transaction manager to use (here the JPA implementation) -->
5<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
6 ...
7</bean>
01import org.springframework.stereotype.Service;
02import org.springframework.transaction.annotation.Transactional;
03import org.springframework.transaction.annotation.Propagation;
04 
05import com.javacodegeeks.Request;
06import com.javacodegeeks.RequestProcessor;
07 
08@Service
09public class RequestProcessorImpl implements RequestProcessor {
10 
11 @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
12 public void process(Request request) throws Exception {
13  ...
14 }
15  
16}
JAVA EE 6
01import javax.ejb.Stateless;
02import javax.ejb.TransactionAttribute;
03import javax.ejb.TransactionAttributeType;
04import javax.ejb.TransactionManagement;
05import javax.ejb.TransactionManagementType;
06 
07import com.javacodegeeks.Request;
08import com.javacodegeeks.RequestProcessor;
09 
10@Stateless
11@TransactionManagement(value=TransactionManagementType.CONTAINER)
12public class RequestProcessorImpl implements RequestProcessor {
13 
14 @TransactionAttribute(TransactionAttributeType.REQUIRED)
15 public void process(Request request) throws Exception {
16  ...
17 }
18  
19}
Be very careful with runtime/unchecked exceptions in Java EE. By default, they are automatically wrapped by the EJB container into an EJBException, which may cause surprising results (especially in try…catch statements!). If you need finer tuning of rollback cases, consider tagging such runtime exceptions as applicative exceptions, either using the @ApplicationException annotation, or by augmenting the ejb descriptor like this:
1<ejb-jar>
2   <assembly-descriptor>
3    <application-exception>
4      <exception-class>java.lang.NullPointerException</exception-class>
5      <rollback>true</rollback>
6    </application-exception>
7   </assembly-descriptor>
8</ejb-jar>

Part IV : Restful web services

Enterprise applications often need to expose some of their services to the outside world, typically through internet. This is where web services are coming into play. Like JMS (for asynchronous communication), web services are another classical integration technique for implementing a synchronous, request-response oriented, communication using XML (or JSON) as exchange format.
SPRING
1<servlet>
2 <servlet-name>ws</servlet-name>
3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4</servlet>
5<servlet-mapping>
6 <servlet-name>ws</servlet-name>
7 <url-pattern>/services/*</url-pattern>
8</servlet-mapping>
1<!-- Dispatch requests to controllers + use JAXB (if found in the classpath) -->
2<mvc:annotation-driven />
01import org.springframework.beans.factory.annotation.Autowired;
02import org.springframework.stereotype.Controller;
03import org.springframework.web.bind.annotation.PathVariable;
04import org.springframework.web.bind.annotation.RequestMapping;
05import org.springframework.web.bind.annotation.RequestMethod;
06import org.springframework.web.bind.annotation.ResponseBody;
07 
08import com.javacodegeeks.Geek;
09import com.javacodegeeks.GeekService;
10 
11@Controller
12@RequestMapping("/geeks")
13public class GeekWebService {
14  
15   @Autowired
16   GeekService bizService;
17    
18  @RequestMapping(value="/{id}", method=RequestMethod.GET)
19  @ResponseBody
20  public Geek getGeek(@PathVariable("id") long geekId) {
21     return bizService.findGeek(geekId);
22 }
23  
24}
01import javax.xml.bind.annotation.XmlAttribute;
02import javax.xml.bind.annotation.XmlElement;
03import javax.xml.bind.annotation.XmlRootElement;
04  
05@XmlRootElement(name="geek")
06public class Geek {
07  
08 private String name;
09 private Long id;
10  
11 @XmlElement
12 public String getName() {
13  return name;
14 }
15  
16 public void setName(String name) {
17  this.name = name;
18 }
19  
20 @XmlAttribute
21 public Long getId() {
22  return id;
23 }
24  
25 public void setId(Long id) {
26  this.id = id;
27 }
28  
29}
JAVA EE 6
01import javax.inject.Inject;
02import javax.ws.rs.GET;
03import javax.ws.rs.Path;
04import javax.ws.rs.PathParam;
05import javax.ws.rs.Produces;
06import javax.ws.rs.core.MediaType;
07 
08import com.javacodegeeks.Geek;
09import com.javacodegeeks.GeekService;
10 
11@Path("/geeks")
12@Produces(MediaType.APPLICATION_XML)
13public class GeekWebService {
14 
15 @Inject
16  GeekService bizService;
17    
18 @GET
19 @Path("/{id}")
20 public Geek getGeek(@PathParam("id") long geekId) {
21    return bizService.findGeek(geekId);
22  }
23   
24}
01import javax.xml.bind.annotation.XmlAttribute;
02import javax.xml.bind.annotation.XmlElement;
03import javax.xml.bind.annotation.XmlRootElement;
04  
05@XmlRootElement(name="geek")
06public class Geek {
07  
08 private String name;
09 private Long id;
10  
11 @XmlElement
12 public String getName() {
13  return name;
14 }
15  
16 public void setName(String name) {
17  this.name = name;
18 }
19  
20 @XmlAttribute
21 public Long getId() {
22  return id;
23 }
24  
25 public void setId(Long id) {
26  this.id = id;
27 }
28  
29}
Remark: some JAX-RS implementations, like JBoss RestEasy, do not require to modify the web.xml in order to configure and install web services…

PART V : Conclusion

Arguing that things are in Spring much simpler, much lighter than in Java EE is not – more exactly, no more – true. It is merely a matter of taste. Furthermore, recent Java EE 6 application servers (like GlassFish 3 or JBoss 6 & 7) are booting really fast, actually nearly as fast as Spring applications. Nevertheless, in a “best-of-breed” perspective, it may still be interesting to combine both technologies; this will be the subject of my next post on JCG