[jboss-cvs] JBossAS SVN: r110922 - projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Mar 15 19:47:33 EDT 2011


Author: marius.bogoevici
Date: 2011-03-15 19:47:33 -0400 (Tue, 15 Mar 2011)
New Revision: 110922

Modified:
   projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Getting_Started_Introductory_Example.xml
   projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Migration_Guide.xml
   projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Spring_On_JBoss_Best_Practices.xml
Log:
Adding extra documentation

Modified: projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Getting_Started_Introductory_Example.xml
===================================================================
--- projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Getting_Started_Introductory_Example.xml	2011-03-15 23:09:57 UTC (rev 110921)
+++ projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Getting_Started_Introductory_Example.xml	2011-03-15 23:47:33 UTC (rev 110922)
@@ -8,7 +8,8 @@
   <title>Getting started - an introductory example</title>
 
   <para>This chapter will provide a quick walkthrough on developing a simple
-  Spring MVC application on JBoss AS, using the JBoss Developer Studio.</para>
+  Spring MVC web application on JBoss AS, using the JBoss Developer
+  Studio.</para>
 
   <section>
     <title>The tools: prerequisites</title>
@@ -17,7 +18,7 @@
 
     <itemizedlist>
       <listitem>
-        <para>Maven 3.0.1 (optional)</para>
+        <para>Maven 3.0.1 (optional, for maven-based examples only)</para>
       </listitem>
 
       <listitem>
@@ -26,7 +27,8 @@
       </listitem>
 
       <listitem>
-        <para>JBoss Developer Studio 3</para>
+        <para>JBoss Developer Studio 3 with m2eclipse (including WTP
+        integration)</para>
       </listitem>
 
       <listitem>
@@ -36,8 +38,391 @@
   </section>
 
   <section>
-    <title>Creating a project</title>
+    <title>Hello World with a database</title>
 
-    <para></para>
+    <para>This is a simple example of a web-based application, including
+    access to a database. In a departure from the typical "Hello World"
+    scenario, the user will be asked to introduce their username in a web
+    form, and the greeting message will contain the full name, extracted from
+    a simple database table. With this, the example will demonstrate:</para>
+
+    <itemizedlist>
+      <listitem>
+        <para>The general project structure of a Spring-based web
+        application</para>
+      </listitem>
+
+      <listitem>
+        <para>A simple set of domain classes, MVC controllers and web
+        pages</para>
+      </listitem>
+
+      <listitem>
+        <para>The data infrastructure configuration (including database, JPA
+        and transactions) for running Spring applications in JBoss</para>
+      </listitem>
+    </itemizedlist>
   </section>
+
+  <section>
+    <title>Creating a Maven-based sample project</title>
+
+    <para>For this example, we will need to create the Maven project
+    infrastructure, add the dependencies, the code and the Spring
+    configuration files. So we will proceed in this order.</para>
+
+    <section>
+      <title>Creating the Maven project structure</title>
+
+      <para>Creating a Maven-based web application from JBDS is fairly easy,
+      if the m2eclipse plugin (both core an WTP integration) is installed.
+      </para>
+
+      <para>Go to New-&gt;Maven Project (may need to select the 'Other' option
+      first). Leave the 'Create a simple project' option unchecked, as we want
+      to use an archetype for setting up the project structure. After clicking
+      'Next', you will be prompted to select an archetype. For this example,
+      we are using org.apache.maven.archetypes:maven-archetype-webapp. After
+      doing so click 'Next' and enter a groupId, artifactId and version (which
+      is also the project name in Eclipse). We are using
+      'org.jboss.spring.getting.started' for the groupId and 'getting-started'
+      for the artifactId, as well as a version number of 1.0 for this example.
+      However, feel free to enter any other values which are more sensible for
+      your environment. We will set the package name to
+      'org.jboss.spring.getting.started' although no package will be created
+      at this point, so the option is not in use.</para>
+
+      <para>So, after doing all this, the end result should look similar to
+      this:</para>
+
+      <para>The artifact may not create the Java source folder, so you may
+      have to add it yourself. Go to 'New'-&gt;Source Folder and enter a value
+      of 'src/main/java'.</para>
+
+      <para>The final step is to add the Spring Project nature to your
+      project. While this is not strictly necessary for your application, it
+      will enable integration with the Spring IDE, which includes visualizing
+      your application context definitions and syntactical and semantical
+      checks.</para>
+    </section>
+
+    <section>
+      <title>Creating the source code</title>
+
+      <para>After doing so, we need to add the source code for the
+      application. The figure illustrates the application components, split in
+      tiers.</para>
+
+      <para>First, we are going to create our domain entity, which contains
+      information about a User. It is a persistent class, so it also contains
+      JPA mappings.</para>
+
+      <programlisting>package org.jboss.spring.getting.started.domain;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+ at Entity
+ at Table(name="USERS")
+public class User {
+
+  @Id
+  private Long id;
+ 
+  @Column private String username;
+
+  @Column String firstName;
+ 
+  @Column String lastName;
+
+  public String getUsername() {
+    return username;
+  }
+
+  public void setUsername(String username) {
+    this.username = username;
+  }
+
+  public String getFirstName() {
+   return firstName;
+  }
+
+  public void setFirstName(String firstName) {
+    this.firstName = firstName;
+  }
+
+  public String getLastName() {
+   return lastName;
+  }
+
+  public void setLastName(String lastName) {
+   this.lastName = lastName;
+ }
+ 
+}</programlisting>
+
+      <para>Data access will be provided through a JPA-based DAO, which will
+      be installed as Spring Bean. The interface for this component is:</para>
+
+      <programlisting>package org.jboss.spring.getting.started.domain;
+
+public interface UserDao {
+  User getForUsername(String username);
+}</programlisting>
+
+      <para>We encourage using plain JPA for the implementation. The class is
+      annotated with @Service, which means that it can be used by Spring to
+      scan for components rather than explicitely defining them in a
+      configuration file. The entity manager that the implementation will use
+      to run JPA queries will be injected in the corresponding field, and
+      @Autowired annotation instructs Spring where to do the injection. The
+      @Transactional annnotation is used for demarcating transactions
+      declaratively, which with this particular setup means that
+      getForUsername() will execute in its own transaction, if a transaction
+      isn't started yet (this may change with other configuration options, as
+      Spring supports different transaction propagation modes). </para>
+
+      <programlisting>@Service
+public class UserDaoImpl implements UserDao {
+
+  @Autowired
+  private EntityManager entityManager;
+
+  @Transactional
+  public User getForUsername(String username) {
+   Query query = entityManager.createQuery("select u from User u where u.username = ?");
+   query.setParameter(1, username);
+   return (User)query.getSingleResult();
+  }
+}</programlisting>
+
+      <para>In the web tier, we will need a controller and a web page for
+      displaying the results. So we will create the UserController class
+      first. </para>
+
+      <programlisting>@Controller
+ at RequestMapping("say-hello")
+public class UserController {
+
+ @Autowired
+ private UserDao userDao;
+ 
+ @RequestMapping(method = RequestMethod.GET)
+ public @ModelAttribute("message") String getInitialMessage()
+ {
+  return "Enter a Valid Name";
+ }
+ 
+ @RequestMapping(method = RequestMethod.POST)
+ public @ModelAttribute("message") String getGreeting(@RequestParam("username") String username)
+ {
+   User user = userDao.getForUsername(username);
+   return "Hello, " + user.getFirstName() + " " + user.getLastName() + "!"q;
+ }
+}</programlisting>
+
+      <para>The @Controller annotation is similar to @Service, but targeted
+      for MVC controller components. It instructs Spring to create a
+      controller bean automatically if it finds the class on the classpath.
+      The @Autowired annotation is similar to the previous example,
+      instructing Spring to inject a UserDao instance automatically. Specific
+      to this class, however, are the MVC annotations: @RequestMapping,
+      @RequestParam and @ModelAttribute.</para>
+
+      <para>We add the @RequestMapping annotation at the class level to map
+      requests that end in 'say-hello' to this controller. The actual URL
+      which will be mapped to it also depends on the rest of the Spring
+      configuration. Spring MVC uses the Front Controller pattern, which means
+      that the requests are dispatched by a servlet, and the mappings in this
+      class are relative to the mapping of the servlet. </para>
+
+      <para>The class serves as backing to a web form, and different methods
+      need to execute when the form page is initially requested (GET) and data
+      is posted back (POST). Hence, the two distinct @RequestMapping
+      annotations with which the getInitialMessage() and getForUsername()
+      methods are annotated. The @ModelAttribute annotation instructs Spring
+      MVC to add the returned value as a request attribute so that it can be
+      used when rendering the view. The @RequestParam instructs the Spring to
+      bind the 'username' value submitted by the form to the method
+      parameter.</para>
+
+      <para>To finalize the code, we will create a view that renders the form.
+      When the page is initially rendered, it will display the warning message
+      returned by getInitialMessage(). After each submission, the page will
+      display a welcome message with the full name of the user. So, create a
+      new JSP page as /src/main/webapp/WEB-INF/views/say-hello.jsp.</para>
+
+      <programlisting>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
+    pageEncoding="UTF-8"%&gt;
+&lt;%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %&gt;
+&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
+&lt;title&gt;Hello&lt;/title&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;form method="post" action="say-hello"&gt;
+Enter username: &lt;input type="text" name="username" value="&lt;c:out value="${username}"/&gt;"&gt;
+&lt;c:out value="${message}"/&gt;
+&lt;p&gt;
+&lt;input type="submit" value="Submit"&gt;
+&lt;/form&gt;
+
+&lt;/body&gt;
+&lt;/html&gt;</programlisting>
+    </section>
+
+    <section>
+      <title>Adding the Spring configuration files</title>
+
+      <para>A Spring MVC application is using the Front Controller pattern
+      (encountered to other frameworks such as Struts, for example). Requests
+      are directed to a servlet which dispatches them to controller
+      components, after which it forwards to a view that renders the response.
+      The Spring configuration consists of two sets of beans which will be
+      bootstrapped in separate application contexts:</para>
+
+      <itemizedlist>
+        <listitem>
+          <para>business logic beans - which configure and wire together
+          services, wrap them in transactional and AOP proxies;</para>
+        </listitem>
+
+        <listitem>
+          <para>front-end components - controllers and URL mappings</para>
+        </listitem>
+      </itemizedlist>
+
+      <para>For configuring the web application, update the web.xml as
+      follows:</para>
+
+      <programlisting>&lt;!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+                         "http://java.sun.com/dtd/web-app_2_3.dtd"&gt;
+&lt;web-app&gt;
+ &lt;display-name&gt;Getting Started with Spring&lt;/display-name&gt;
+ &lt;context-param&gt;
+  &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
+  &lt;param-value&gt;/WEB-INF/spring-business-context.xml&lt;/param-value&gt;
+ &lt;/context-param&gt;
+ &lt;listener&gt;
+  &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
+ &lt;/listener&gt;
+ &lt;servlet&gt;
+  &lt;servlet-name&gt;Spring MVC Servlet&lt;/servlet-name&gt;
+  &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
+  &lt;init-param&gt;
+   &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
+   &lt;param-value&gt;/WEB-INF/spring-mvc-context.xml&lt;/param-value&gt;
+  &lt;/init-param&gt;
+ &lt;/servlet&gt;
+ 
+ &lt;servlet-mapping&gt;
+  &lt;servlet-name&gt;Spring MVC Servlet&lt;/servlet-name&gt;
+  &lt;url-pattern&gt;/app/*&lt;/url-pattern&gt;
+ &lt;/servlet-mapping&gt;
+
+&lt;/web-app&gt;
+</programlisting>
+
+      <para>The ContextLoaderListener will boostrap an application context
+      containing the business components, while the DispatcherServlet will
+      create an application context using the former as its parent (and thus
+      having access to its bean definitions). </para>
+
+      <para>From src/main/webapp/WEB-INF, create a Spring bean definition
+      file, by selecting New-&gt;Spring Bean Definition File and enter a file
+      name of 'spring-business-context.xml'. Make sure that you follow the
+      'Next' option and select the following namespaces: bean, context, jbdc,
+      jee and tx. The content of the file is as follows;</para>
+
+      <programlisting>&lt;beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:jee="http://www.springframework.org/schema/jee"
+ xmlns:jdbc="http://www.springframework.org/schema/jdbc"
+ xmlns:tx="http://www.springframework.org/schema/tx"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
+ http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
+ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
+ http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
+ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
+ 
+ &lt;context:component-scan base-package="org.jboss.spring.getting.started.domain"/&gt;
+ 
+ &lt;jee:jndi-lookup jndi-name="java:/DefaultDS" id="dataSource" expected-type="javax.sql.DataSource"/&gt;
+ 
+ &lt;jee:jndi-lookup jndi-name="java:/hello/EntityManager" id="entityManager" expected-type="javax.persistence.EntityManager"/&gt;
+ 
+ &lt;tx:jta-transaction-manager/&gt;
+ 
+ &lt;tx:annotation-driven/&gt;
+ 
+ &lt;jdbc:initialize-database data-source="dataSource"&gt;
+     &lt;jdbc:script location="classpath*:init-db.sql"/&gt;
+ &lt;/jdbc:initialize-database&gt;
+ 
+&lt;/beans&gt;</programlisting>
+
+      <para>First, we instructed Spring to scan the package
+      'org.jboss.spring.getting.started.domain' for component beans, looking
+      for specific annotations such as @Service, and inject them automatically
+      based on the @Autowired annotation. In our case, this will create the
+      bean definition for UserDao. Then, we create beans for the data source
+      and the entity manager. Because we are using a managed data source and a
+      container-managed entity manager. The management strategy is set to JTA,
+      and we instruct Spring to apply transactions declaratively, based on the
+      @Transactional annotations found on bean classes. Since this is a demo
+      example, we need to initialize the database and we instruct Spring to
+      run the init-db.sql script at startup.</para>
+
+      <para>The front-end component that are used to configure the dispatcher
+      servlet are defined in a file named 'spring-mvc-context.xml'. Follow the
+      same steps as with the business context and select the namespace bean,
+      context and mvc in the dialog. The configuration file is structured as
+      follows:</para>
+
+      <programlisting>&lt;beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:mvc="http://www.springframework.org/schema/mvc"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
+ http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;
+
+ &lt;context:component-scan base-package="org.jboss.spring.getting.started.mvc"/&gt;
+ 
+ &lt;mvc:annotation-driven/&gt;
+ 
+ &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
+ &lt;property name="prefix" value="/WEB-INF/views/"/&gt;
+ &lt;property name="suffix" value=".jsp"&gt;&lt;/property&gt;
+ &lt;/bean&gt;
+&lt;/beans&gt;
+</programlisting>
+
+      <para>Using this context definition, Spring will scan for component
+      beans (such as the UserController), and will use the mappings defined by
+      the annotations found on controller beans. Finally, after a controller
+      is executed, the InternalResourceViewResolver will determine a jsp page
+      that DispatcherServlet will forward the request for rendering the
+      response.</para>
+
+      <para></para>
+    </section>
+  </section>
+
+  <section>
+    <title>Creating a simple Eclipse project</title>
+
+    <para>A simple (non-Maven based) Eclipse project will be similar to the
+    Maven-based one. The content of the component classes and bean definitions
+    is identical, the main difference being the location of source folders and
+    the need to manually add the dependencies to the project and selecting the
+    ones that need to be exported (i.e. included in the deployed WAR.</para>
+  </section>
 </chapter>

Modified: projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Migration_Guide.xml
===================================================================
--- projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Migration_Guide.xml	2011-03-15 23:09:57 UTC (rev 110921)
+++ projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Migration_Guide.xml	2011-03-15 23:47:33 UTC (rev 110922)
@@ -16,6 +16,23 @@
   <section>
     <title>Spring's PetClinic</title>
 
+    <para>The migration of the default Petclinic example to JBoss is fairly
+    simple and involves the following steps:</para>
+
+    <itemizedlist>
+      <listitem>
+        <para>switching to a managed datasource;</para>
+      </listitem>
+
+      <listitem>
+        <para>switching transaction management strategy to JTA;</para>
+      </listitem>
+    </itemizedlist>
+  </section>
+
+  <section>
+    <title>Spring Travel</title>
+
     <para></para>
   </section>
 </chapter>

Modified: projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Spring_On_JBoss_Best_Practices.xml
===================================================================
--- projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Spring_On_JBoss_Best_Practices.xml	2011-03-15 23:09:57 UTC (rev 110921)
+++ projects/docs/enterprise/WFK/Spring_Developer_Guide/en-US/Spring_On_JBoss_Best_Practices.xml	2011-03-15 23:47:33 UTC (rev 110922)
@@ -10,8 +10,8 @@
   <para>The Spring Framework is a flexible environment for developing
   enterprise applications, with respect to the middleware integration.This
   means that the developer has the benefit of being able to choose between a
-  number of alternatives when it comes to accessing middleware services.
-  </para>
+  number of alternatives when it comes to accessing middleware
+  services.</para>
 
   <para>JBoss is a feature-rich application server, that provides a complex
   and performant set of middleware services. Spring developers can take
@@ -89,14 +89,14 @@
     </section>
 
     <section>
-      <title>Defining a Hibernate SessionFactory</title>
+      <title>Defining and using a Hibernate SessionFactory</title>
 
       <para>Applications that use Hibernate directly (as opposed to JPA)
       should take advantage of the fact that Hibernate is already included in
       the application server. Therefore, applications do not need to include a
       Hibernate distribution.</para>
 
-      <para>Spring applications can one of the Spring's
+      <para>Spring applications can use one of the Spring's
       SessionFactory-instantiating FactoryBeans, and a managed datasource (see
       previous section for details).</para>
 
@@ -119,10 +119,29 @@
 hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup
 hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory</programlisting>
       </example>
+
+      <para>Once such a SessionFactory has been defined, it can be injected in
+      component classes such as DAOs and used directly as in the following
+      example.</para>
+
+      <example>
+        <title>Hibernate-based DAO: a SessionFactory is injected directly in
+        the bean</title>
+
+        <para><programlisting>public class HibernateAccountDao 
+{
+  @Autowired SessionFactory sessionFactory;
+
+  public List&lt;Account&gt; getAllAccounts() {
+     return sessionFactory.getCurrentSession().getCriteria(Account.class).list();
+  }
+  ...
+}</programlisting></para>
+      </example>
     </section>
 
     <section>
-      <title>Defining JPA persistence unit</title>
+      <title>Defining a JPA persistence unit</title>
 
       <para>Spring applications that use JPA are advised to managed
       persistence unit and access them from JNDI. In order to bind the entity
@@ -147,7 +166,8 @@
       <para>Spring applications can use either container-managed entity
       managers or, if necessary, application-managed entity managers (case in
       which they will be provided with the corresponding entity manager
-      factories).</para>
+      factories). A container-managed entity manager can be accessed as a
+      Spring Bean as follows:</para>
 
       <example>
         <title>Spring bean representing a container-managed entity
@@ -156,44 +176,185 @@
         <programlisting>&lt;jee:jndi-lookup id="entityManager" jndi-name="java:/example/em"/&gt;</programlisting>
       </example>
 
+      <para>Such an EntityManager can be used directly if injected in
+      component classes such as DAOs. When doing so, the</para>
+
       <example>
+        <title>Hibernate-based DAO: a SessionFactory is injected directly in
+        the bean</title>
+
+        <para><programlisting>public class HibernateAccountDao 
+{
+  @Autowired EntityManager entityManager;
+
+  public List&lt;Account&gt; getAllAccounts() {
+     return return entityManager.createQuery("SELECT a FROM Account").getResultList();;
+  }
+  ...
+}</programlisting></para>
+      </example>
+
+      <para>An EntityManagerFactory can also be used directly, when the
+      scenario requires an application-managed entityManager.</para>
+
+      <example>
         <title>Spring bean representing an entity manager factory</title>
 
         <programlisting>&lt;jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/example/emf"/&gt;</programlisting>
       </example>
+
+      <para>In general, for implementing transaction-aware components (for
+      example a service that delegates to multiple DAOs which have to be
+      enrolled in the same transaction), it is not typical to access
+      EntityManagerFactories directly. Rather, components should access the
+      JNDI-bound EntityManager. However, EntityManagerFactory beans are
+      typically used in scenarios that span multiple transactions such as
+      conversations. For example, Spring Web Flow requires access to an
+      EntityManager in order to provide a conversation-scoped persistence
+      context. In such a case, you can use the JNDI-bound
+      EntityManagerFactory.</para>
+
+      <para>You can find a more detailed usage example in the Sportsclub
+      application.</para>
     </section>
+  </section>
 
-    <section>
-      <title>Transaction management</title>
+  <section>
+    <title>Messaging (JMS) integration</title>
 
-      <para>The recommended transaction management strategy for JBoss is by
-      using JTA whose performance is close to native JDBC in single-resource
-      scenarios and XA-capable in multiple-resource scenarios. One of the
-      significant advantages of JTA is integration with other components of
-      the application stack - such as EJBs and third-party components. </para>
+    <para>Spring applications have two distinct mechanisms of integrating with
+    JMS. One is by using the JmsTemplate which accessing the JMS API directly
+    for both sending and receiving messages, and the other is by implementing
+    message-driven POJOs. The best practice in JBoss is to use the JmsTemplate
+    for sending messages but not for receiving them, as the
+    message-driven/event-driven paradigm is a superior approach both as a
+    programming style and performance-wise than polling/blocking via
+    JmsTemplate.receiveMessage().</para>
 
-      <para>This can be simply achieved by using the standard Spring JTA
-      transaction manager definition. </para>
+    <para>Spring applications can implement message-driven POJOs by wiring
+    MessageListener beans (or POJOs via a MessageListenerAdapter) into a
+    MessageListenerContainer. MessageListenerContainers come in two flavours:
+    'native' JMS and JCA-based. On JBoss AS we recommend using the JCA
+    MessageListenerContainer, due to the better integration with the
+    container. In order to minimize the amount of proprietary code, you can
+    use Snowdrop's namespace support for JMS/JCA integration.</para>
 
-      <example>
-        <title>JTA transaction manager definition in Spring</title>
+    <example>
+      <title>Using the JCA message listener containers and namespace support
+      in JBoss</title>
 
-        <programlisting>&lt;tx:jta-transaction-manager/&gt;</programlisting>
-      </example>
+      <para><programlisting> &lt;jms:jca-listener-container resource-adapter="resourceAdapter" acknowledge="auto"
+                                activation-spec-factory="activationSpecFactory"&gt;
+        &lt;jms:listener destination="/someDestination" ref="messageDrivenPojo"
+                      method="pojoHandlerMethod"/&gt;
+    &lt;/jms:jca-listener-container&gt;
 
-      <para>The use of this transaction manager allows Spring to create JTA
-      transactions (for example, through Spring declarative transaction
-      management) or to enroll in existing transactions, if any. It requires
-      the use of managed datasources, JTA-integrated session factories or
-      container-deployed persistence units and ensures that the underlying
-      database connections, sessions and persistence contexts are managed
-      transparently and shared with other components.</para>
-    </section>
+
+    &lt;jboss:activation-spec-factory id="activationSpecFactory" subscriptionName="jca-example" useDLQ="false"/&gt;
+
+    &lt;jboss:resource-adapter id="resourceAdapter"/&gt;
+
+    &lt;bean id="messageDrivenPojo" class="example.MessageDrivenPojo"/&gt;</programlisting></para>
+    </example>
+
+    <para>Whenever a message arrives to the destination, it will be converted
+    to the argument type of the method <emphasis>pojoHandlerMethod</emphasis>
+    of the messageDrivenPojo bean which will be invoked by the container. Any
+    regular bean can be a message-driven POJO, the only restriction beigng
+    that the handling method must have a single argument. Spring will take
+    care of converting message content to the expected argument type. For a
+    JCA-based container, the invocation will be automatically enrolled in a
+    JTA transaction.</para>
+
+    <para>A message-driven POJO example is provided in the following
+    listing:</para>
+
+    <para><example>
+        <title>A message-driven POJO is a regular bean type</title>
+
+        <para></para>
+
+        <programlisting>
+public class MessageDrivenPojo
+{
+   @Autowire PaymentProcessor paymentProcessor;
+
+
+   public void pojoHandlerMethod(PaymentNotification paymentNotification)
+   {
+      paymentProcessor.processPayment(paymentNotification.getAccountNumber(), paymentNotification.getAmount());
+
+   }
+
+}
+</programlisting>
+      </example></para>
   </section>
 
   <section>
-    <title>Messaging integration</title>
+    <title>Transaction management</title>
 
+    <para>The recommended transaction management strategy for JBoss is by
+    using JTA whose performance is close to native JDBC in single-resource
+    scenarios and XA-capable in multiple-resource scenarios. One of the
+    significant advantages of JTA is integration with other components of the
+    application stack - such as EJBs and third-party components.</para>
+
+    <para>Spring provides a declarative transaction model including
+    transaction propagation semantics, so that applications can declare
+    various methods as being automatically enrolled in a transaction. We
+    provided an example of doing this in the previous chapter and a more
+    extensive example can be found in the Sportsclub user guide. The model is
+    the same regardless whether the transactions being used are local
+    transactions or JTA transactions. This makes the switch to JTA fairly
+    easy, as the core of the application is the same, the only component that
+    changes being the transaction manager type.</para>
+
+    <para>For JTA in a Spring application, you can simply use the standard
+    Spring JTA transaction manager definition.</para>
+
+    <example>
+      <title>JTA transaction manager definition in Spring</title>
+
+      <programlisting>&lt;tx:jta-transaction-manager/&gt;</programlisting>
+    </example>
+
+    <para>The use of this transaction manager allows Spring to create JTA
+    transactions (for example, through Spring declarative transaction
+    management) or to enroll in existing transactions, if any. It requires the
+    use of managed datasources, JTA-integrated session factories or
+    container-deployed persistence units and ensures that the underlying
+    database connections, sessions and persistence contexts are managed
+    transparently and shared with other components.</para>
+
+    <para>Please note that in the case of message-driven beans, the message
+    handling invocations are already enrolled in a JTA transaction. </para>
+  </section>
+
+  <section>
+    <title>Web Services integration (JAX-WS)</title>
+
+    <para>Spring applications can create web services in a variety of way. In
+    the simplest case, Spring applications can register JAX-WS web services
+    out of the box, by simply defining an annotated class and registering it
+    as a servlet.</para>
+
     <para></para>
   </section>
+
+  <section>
+    <title>EJB integration</title>
+
+    <para></para>
+  </section>
+
+  <section>
+    <title>Security integration</title>
+
+    <para></para>
+
+    <para></para>
+  </section>
+
+  <section></section>
 </chapter>



More information about the jboss-cvs-commits mailing list