DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Trending

  • Securing the Future: Best Practices for Privacy and Data Governance in LLMOps
  • System Coexistence: Bridging Legacy and Modern Architecture
  • Start Coding With Google Cloud Workstations
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  1. DZone
  2. Coding
  3. Frameworks
  4. Connecting to EJBs from Spring

Connecting to EJBs from Spring

By 
Łukasz Budnik user avatar
Łukasz Budnik
·
Jun. 24, 12 · Interview
Likes (16)
Comment
Save
Tweet
Share
30.9K Views

Join the DZone community and get the full member experience.

Join For Free
While preparing my next exercise I had to first find a way how to connect to a remote SLSB from Spring.

It turned out to be childishly simple. See how I did it.

Writing SLSB component

First I needed a remote SLSB.

Using M2Eclipse create a Maven 2 project. From the archetypes selection window I typed mojo in filter field (mojo archetypes are codehaus archetypes, I wrote about them in: Don't use org.apache.maven.archetypes!) and selected ejb3 archetype.

I wrote a simple SLSB component:
package org.xh.studies.openejb;
import javax.ejb.Remote;
@Remote
public interface Greeter {
 String sayHello(String to);
}

package org.xh.studies.openejb;
import javax.ejb.Stateless;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@Stateless
public class GreeterImpl implements Greeter { 
 private static final Log log = LogFactory.getLog(GreeterImpl.class);
 public String sayHello(String to) {
  log.info("Saying hi to: " + to);
  return "Hello " + to + "! How are you?";
 }
}
I built it with:
mvn package
Deploying to OpenEJB

I downloaded OpenEJB archive from here: http://openejb.apache.org/download.html and unzipped it. I was done with installation :)

I copied built in previous step jar to apps directory, then went to bin directory and ran openejb(.bat).

Writing JUnit integration test

First I wrote a simple integration test (using failsafe plugin, there are many post on my blog about it, the key one is: Maven2 integration testing with failsafe and jetty plugins) to verify if the SLSB is actually working:
public class GreeterIT {
 private static Context ctx;
 @BeforeClass
 public static void setUp() throws NamingException {
  Properties env = new Properties();
  env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
  env.put(Context.PROVIDER_URL, "ejbd://localhost:4201");
  ctx = new InitialContext(env);
 }
 @AfterClass
 public static void tearDown() throws NamingException {
  ctx.close();
 }
 @Test
 public void sayHelloRemoteEJBTest() throws NamingException {
  Greeter greeter = (Greeter) ctx.lookup("GreeterImplRemote");
  String result = greeter.sayHello("Łukasz");
  String expected = "Hello Łukasz! How are you?";
  Assert.assertEquals(expected, result);
 }
}
I ran the test and it passed.

Connecting to EJB from Spring

I found out a Spring component called org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean. It uses AOP behinde the scenes to create a proxy for a remote SLSB. All you need is just an interface.

First I had to add Spring dependencies to my pom.xml. These were:
spring-beans
spring-context
spring-aop
Then I created a file called META-INF/spring/beans.xml and wrote:
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
  <property name="environment">
   <props>
    <prop key="java.naming.factory.initial">org.apache.openejb.client.RemoteInitialContextFactory</prop>
    <prop key="java.naming.provider.url">ejbd://localhost:4201</prop>
   </props>
  </property>
 </bean>
 <bean id="greeterBean"
  class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
  <property name="jndiName" value="GreeterImplRemote" />
  <property name="jndiTemplate" ref="jndiTemplate" />
  <property name="businessInterface" value="org.xh.studies.openejb.Greeter" />
 </bean>
</beans>
In the above file I created a greeterBean which was a dynamic proxy and, thanks to AOP, would behave like a Greeter object.

Writing Spring test

I added one more field to my test:
private static ApplicationContext springCtx;
Then in @BeforeClass class I added ApplicationContext initialisation code:
springCtx = new ClassPathXmlApplicationContext("META-INF/spring/beans.xml");
Finally I wrote a new method to test what I intended to do first, connecting to a remote SLSB from Spring:
@Test
public void sayHelloSpringProxyTest() throws NamingException {
 Greeter greeter = (Greeter) springCtx.getBean("greeterBean");
 String result = greeter.sayHello("Łukasz");
 String expected = "Hello Łukasz! How are you?";
 Assert.assertEquals(expected, result);
}
It worked!

Source code download

A complete source code download can be found here: Connecting-to-EJB-from-Spring.zip.

Summary

Isn't it beautiful? You get an instance of a remote SLSB as a Spring bean. I definitely like it!

thanks,
Łukasz



Spring Framework

Published at DZone with permission of Łukasz Budnik, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!