paulwong

          Spring3 and EJB Integration(I)EJB Client

          Spring3 and EJB Integration(I)EJB Client
          I used EJB client to call EJB stateless bean, I found it is waste time to lookup the stateless bean every time.
          So I tried to use spring to get singleton stub from EJB stateless bean.
          The sample project are in easyaxis2proxy,easyrestproxy,easyjpa,easyejb.
          We can configure that in Remote/Local way, you can choose that from comment/uncomment the codes I mentioned here.

          1. Spring configuration file
          local-ejb-context.xml:
          <beans xmlns="http://www.springframework.org/schema/beans" 
          xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
          xmlns:tx
          ="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 
          xsi:schemaLocation
          ="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
                     http://www.springframework.org/schema/tx 
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          "
          > 
          <bean id="jndiTemplate" 
                  class
          ="org.springframework.jndi.JndiTemplate"> 
                  
          <property name="environment"> 
                      
          <props> 
                          
          <prop key="java.naming.provider.url"> 
                              ${java.naming.provider.url} 
                          
          </prop> 
                          
          <prop key="java.naming.factory.initial"> 
                              ${java.naming.factory.initial} 
                          
          </prop> 
                          
          <prop key="java.naming.factory.url.pkgs"> 
                              ${java.naming.factory.url.pkgs} 
                          
          </prop> 
                      
          </props> 
                  
          </property> 
              
          </bean> 
          <bean id="userServiceLocal" 
          class
          ="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean"> 
          <property name="jndiTemplate" ref="jndiTemplate" /> 
          <property name="jndiName" value="easyejb/UserServiceLocal" /> 
          <property name="businessInterface" 
          value
          ="com.sillycat.core.webservice.interfaces.UserServiceLocal" /> 
          </bean> 
          </beans> 

          remote-ejb-context.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
          xmlns:tx
          ="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation
          ="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          "
          >

          <bean id="jndiTemplate"
          class
          ="org.springframework.jndi.JndiTemplate">
          <property name="environment">
          <props>
          <prop key="java.naming.provider.url">
          ${java.naming.provider.url}
          </prop>
          <prop key="java.naming.factory.initial">
          ${java.naming.factory.initial}
          </prop>
          <prop key="java.naming.factory.url.pkgs">
          ${java.naming.factory.url.pkgs}
          </prop>
          </props>
          </property>
          </bean>
          <bean id="userServiceRemote" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
          <property name="jndiTemplate" ref="jndiTemplate" />
          <property name="jndiName" value="easyejb/UserService" />
          <property name="businessInterface"
          value
          ="com.sillycat.core.webservice.interfaces.UserService" />
          </bean>
          </beans>

          We can choose use Remote/Local from the main-context.xml

          <import resource="classpath:resource-context.xml" />
          <import resource="classpath:core-context.xml" />
          <import resource="classpath:dao-context.xml" />
          <import resource="classpath:manager-context.xml" />
          <!--
          <import resource="classpath:local-ejb-context.xml" />
          <import resource="classpath:remote-ejb-context.xml" />
          -->
          <import resource="classpath:local-ejb-context.xml" />

          <import resource="classpath:controller-easyrestproxy-context.xml" />
          <import resource="classpath:restclient-context.xml" />

          <!--My manager spring configuration to call EJB -->
          <bean id="personManager" class="com.sillycat.easyjpa.manager.PersonManagerImpl" >
          <property name="personDAO" ref="personDAO" />
          <!--
          <property name="userServiceLocal" ref="userServiceLocal" />
          <property name="userServiceRemote" ref="userServiceRemote" />
          -->
          <property name="userServiceLocal" ref="userServiceLocal" />
          </bean>

          2. The properties file config.properties:

          ###########################################
          # EJB configuration
          ###########################################
          java.naming.factory.initial
          =org.jnp.interfaces.NamingContextFactory
          java.naming.factory.url.pkgs
          =org.jboss.naming:org.jnp.interfaces
          java.naming.provider.url
          =localhost:1099

          3. My Manager java source:

          package com.sillycat.easyjpa.manager;
          import java.util.List;
          import com.sillycat.core.webservice.interfaces.UserServiceLocal;
          import com.sillycat.core.webservice.model.IUser;
          import com.sillycat.easyjpa.dao.PersonDAO;
          import com.sillycat.easyjpa.model.Person;
          public class PersonManagerImpl implements PersonManager
          {
          PersonDAO personDAO;
          UserServiceLocal userServiceLocal;
          // UserService userServiceRemote;
          public List<Person> getAll()
          {
          return personDAO.getAll();
          }

          public Person get(Integer id)
          {
          IUser iuser
          = userServiceLocal.get(Integer.valueOf(1));
          // IUser iuser = userServiceRemote.get(Integer.valueOf(1));
          Person p = personDAO.get(id);
          p.setName(iuser.getEmail());
          return p;
          }

          public void save(Person person)
          {
          if (person != null && person.getId() != null)
          {
          personDAO.update(person);
          }

          else
          {
          personDAO.insert(person);
          }

          }

          public void updateName(Person person)
          {
          if (person != null && person.getId() != null)
          {
          personDAO.updateName(person.getName(), person.getId());
          }

          }

          public void delete(Integer id)
          {
          personDAO.delete(id);
          }

          public void setPersonDAO(PersonDAO personDAO)
          {
          this.personDAO = personDAO;
          }

          // public void setUserServiceRemote(UserService userServiceRemote)
          // {
          // this.userServiceRemote = userServiceRemote;
          // }
          public void setUserServiceLocal(UserServiceLocal userServiceLocal)
          {
          this.userServiceLocal = userServiceLocal;
          }

          }


          4. We can deploy these packages to JBOSS, and run the axis2 testcase or rest testcase to verify that.

          posted on 2011-11-24 21:54 paulwong 閱讀(537) 評論(0)  編輯  收藏 所屬分類: EJB3

          主站蜘蛛池模板: 新宁县| 麦盖提县| 洛扎县| 平乐县| 唐山市| 三门峡市| 海原县| 两当县| 五指山市| 格尔木市| 将乐县| 旬邑县| 镇宁| 望都县| 通榆县| 邹城市| 凤翔县| 察雅县| 扬州市| 永定县| 醴陵市| 安化县| 将乐县| 新竹市| 太原市| 韶关市| 襄城县| 辽中县| 田林县| 康定县| 开原市| 保定市| 武汉市| 堆龙德庆县| 西畴县| 元江| 岫岩| 莱阳市| 新龙县| 饶平县| 达日县|