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 阅读(525) 评论(0)  编辑  收藏 所属分类: EJB3


只有注册用户登录后才能发表评论。


网站导航: