Over a million developers have joined DZone.

OpenEJB ApplicationComposer and Random Port

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

ApplicationComposer is a great way to test a small JAXRS service but how to handle concurrent builds? how to avoid port conflicts since all builds will use the same (default) port?

An easy solution is to “affect” one port by build…but don’t ask me to maintain the range allocation ;).

OpenEJB now provides a nice solution.

As a quick reminder here is how to test a simple webservices (I suppose you have openejb-cxf-rs as test dependency):


@EnableServices("jaxrs")
public class MyTest {
    @Rule
    public final ApplicationComposerRule app = new ApplicationComposerRule(this);
 
    @Module
    @JaxrsProviders(JohnzonProvider.class) // optional
    @Classes(cdi = true, value = { MyResource.class, MyService.class })
    public WebApp webapp() {
        return new WebApp();
    }
 
    @Test
    public void theTest() throws IOException {
        // resource accesible on http://localhost:4204/openejb/
    }
}

Of course you can provide a @Configuration method and set a random port as value for the property httpejb.port but this is not as “fluent” as it should be. That’s why now you can use @RandomPort feature:

@EnableServices("jaxrs")
public class MyTest {
    @RandomPort("httpejbd")
    private URL httpEjbdPort; // http://localhost:{port}/
 
    // as before
}

@RandomPort takes the service name as parameter (if existing it will set the openejb service port otherwise name is just a key). If you inject twice the same service you’ll get the same port. Finally you can get the port injected if you type the field as an int or the root context using URL type.

Happy testing of your microservices with OpenEJB and TomEE!

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}