<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>breddy.net &#187; spring</title>
	<atom:link href="http://www.breddy.net/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.breddy.net</link>
	<description>Personal and professional weblog of Chris Bredesen</description>
	<lastBuildDate>Tue, 21 Jun 2011 20:31:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Accessing a JBoss EJB3 Session Bean With Spring</title>
		<link>http://www.breddy.net/2006/07/06/accessing-a-jboss-ejb3-session-bean-with-spring/</link>
		<comments>http://www.breddy.net/2006/07/06/accessing-a-jboss-ejb3-session-bean-with-spring/#comments</comments>
		<pubDate>Thu, 06 Jul 2006 17:34:09 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Professional]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.breddy.net/2006/07/06/accessing-a-jboss-ejb3-session-bean-with-spring/</guid>
		<description><![CDATA[Spring and Hibernate have long been my choice of frameworks with which to construct maintainable, scalable middle-tier software. Spring promotes good OO design using loose coupling and provides excellent declarative transaction support. Hibernate is the persistence tool of choice for &#8230; <a href="http://www.breddy.net/2006/07/06/accessing-a-jboss-ejb3-session-bean-with-spring/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Spring and Hibernate have long been my choice of frameworks with which to construct maintainable, scalable middle-tier software.  Spring promotes good OO design using loose coupling and provides excellent declarative transaction support.  Hibernate is the persistence tool of choice for the open source community.  Any sane person who programs this way will have given up on EJB long ago.  Coarse-grained entities, tightly coupled service objects and XML deployment descripters a-plenty are enough to bring a guy to his knees.</p>
<p>But no more.  The EJB expert group, now composed of some of the very creators of modern persistence framework, has bequeathed upon us something wonderful.  The lightweight POJO programming model we have come to love is apparent everywhere in the EJB 3.0 specifiction (<a title="JSR-220" href="http://www.jcp.org/en/jsr/detail?id=220" target="_blank">JSR-220</a>), which was <a title="JSR-220 voting results" href="http://www.jcp.org/en/jsr/results?id=3768" target="_blank">unanimously accepted</a> by the <a title="Java Community Process" href="http://www.jcp.org" target="_blank">JCP</a> earlier this year.  EJB 3.0 is just one goodie in the larger <a title="Java EE 5 What's New" href="http://java.sun.com/javaee/overview/whatsnew.jsp" target="_blank">Java EE 5</a> (<a title="JSR-244" href="http://www.jcp.org/en/jsr/detail?id=244" target="_blank">JSR-244</a>) bag &#8211; but this post is going to be long enough as it is.<span id="more-40"></span></p>
<p>When I returned from Las Vegas with my newfound interest in EJB, my first task at hand was to create a little test application that demonstrated the scenario that is most applicable to my work:  EJB3 services remoted over RMI consumed with POJO web applications that already use Spring.  This use case stems from the need that we have realized the need to segment a growing middle-tier application from the monolithic beast it is destined to become into smaller, more manageable services.  The client programs (mostly web applications) already access the business interfaces using Spring.</p>
<p>I&#8217;m going to skip the gory details of setting up the server, creating a project and all the like.  There are plenty of explanations of this elsewhere.  Besides, this post is going to be long enough as it is.</p>
<p>First we need a business interface.  How about something really complex:</p>
<p>[java]<br />
public interface SimpleService {<br />
int stringLength(String string);<br />
}<br />
[/java]</p>
<p>Nothing fancy there.  Now let&#8217;s implement it with some logic:</p>
<p>[java]<br />
public class SimpleServiceBean implements SimpleService {<br />
public int stringLength(String string) {<br />
return string == null ? 0 : string.length();<br />
}<br />
}<br />
[/java]</p>
<p>Still totally straightforward.  Now let&#8217;s turn our bean into an EJB.  This is the <em>really</em> complicated part.  Watch for the annotations:</p>
<p>[java]<br />
@Stateless<br />
@Remote(SimpleService.class)<br />
public class SimpleServiceBean implements SimpleService {<br />
public int stringLength(String string) {<br />
return string == null ? 0 : string.length();<br />
}<br />
}<br />
[/java]</p>
<p>Did you get all that?  The <tt>@Stateless</tt> annotation tells us that this is a Stateless Session bean, and the <tt>@Remote</tt> annotation specifies the remote interface.  <em>Everything else is handled by the container.</em> Although the footwork is managed by the container in a somewhat vendor-specific way, the annotations are standard Java EE, and so you&#8217;re not tied to an implementation. When you catch your breath, let&#8217;s move on.</p>
<p>Since Java EE 5 has embraced configuration by exception, we don&#8217;t even need any depoyment descriptors.  These two files can be compiled, JAR&#8217;d up and deployed to a server of your choosing &#8212; I used JBoss 4.0.4.</p>
<p>In order to have a client, we need to provide, at a minimum, the remote interfaces and any other classes or exceptions it references.  For testing, I just tossed the same JAR on the classpath since I know it contains everything I need.  Additionally, we need the Spring JARs (this test used 2.0RC1) and the JBoss client JAR&#8217;s.</p>
<p>The only real programming we have to do here is to create a Spring container that wires up to the server&#8217;s JNDI directory.  To do this, we&#8217;ll use Spring&#8217;s <tt>JndiObjectFactoryBean</tt>.  This part actually confused me because try as I did, I could never accomplish this feat using the more aptly-named <tt>SimpleRemoteStatelessSessionProxyFactoryBean</tt>.  The difference, apparently, is that since EJB3 places no specific requirements on the business, home or remote interfaces, there is less to do, and thus the client simply grabs the object and narrows it down as prescribed.  Here is my Spring configuration (minus the XML header and DTD info):</p>
<p>[xml]</p>
<p>org.jnp.interfaces.NamingContextFactory org.jboss.naming:org.jnp.interfaces jnp://localhost:1099<br />
[/xml]</p>
<p>The JNDI properties provided are JBoss specific, as is the provider URL.  These are included with the JBoss client JAR in the form of a jndi.properties file.  They are exactly the same as what would be used to construct a traditional <tt>InitialContext</tt> if this were a non-spring J2SE client.</p>
<p>JBoss mounts Remote interfaces (by default) using /remote, and Local interfaces using /local.  As you can see here, we&#8217;ve chosen the remote.  One could just as easily expose <tt>SimpleServiceBean</tt> as a Local EJB by using the <tt>@Local</tt> annotation and providing the same business interface as an arguement.  The Spring container changes only in URL and jndiName values.</p>
<p>Once the bean factory is configured, you can load it and request the remote <tt>SimpleServiceBean</tt> proxy by name (simpleService), casting it to <tt>SimpleService</tt> as you would normally do with a Spring-managed object.  It works flawlessly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.breddy.net/2006/07/06/accessing-a-jboss-ejb3-session-bean-with-spring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

