users@glassfish.java.net

[gf-users] Re: running embedded glassfish for unit testing

From: Nigel Deakin <nigel.deakin_at_oracle.com>
Date: Wed, 08 Apr 2015 12:00:31 +0100

Etienne,

On 05/04/2015 23:03, Etienne Marin-Matholaz wrote:
> Hi all,
> I'm using Glassfish, and more precisely its JMS implementation to send and receive messages between my java
> applications. To do so, I'm running Glassfish in standalone mode in Netbeans, and everything works like a charm. I don't
> know anything about Message Driven Beans, Enterprise Java Beans, and Web applications, I'm just writing a simple Java
> code with JMSConsumers, JMSProducers and JMSContext, and it's enough for my java applications to be able to communicate
> between themselves.

If you're not running a MDB, EJB or servlet, how are you running your "simple java code"? It'll be easier to help you if
you say about more about this.

> Now, I would like to test the JMS communication using Java unit tests, and without having a Glassfish server running in
> standalone. I found here : http://docs.oracle.com/cd/E18930_01/html/821-2424/giijw.html#scrolltoc how to run an instance
> of Glassfish embedded in my test code.
>
> But now I get the following runtime errors, because of the setClientID method of JMSContext and the setMessageListener
> method of JMSConsumer :
> "This method may not be called in a Java EE web or EJB container"
>
> Looking at the doc, it seems indeed that those methods cannot be used in a Java EE web application or EJB container. In
> other words, my code seems not to be portable, whereas I thought it was standard and pretty simple JMS that I was writing.

There are various JMS methods which can be used in a standalone Java client (or the application client container) but
which are not permitted in the server (e.g. as an EJB servlet etc), since some parts of the JMS API are considered
inappropriate in a more managed Java EE environment where you have things like connection pooling, XA transactions,
thread pooling and so on. You can question whether these restrictions are inappropriate, but that's what the Java EE and
JMS spec have defined for many years.

The opposite applies too: code written for use in the server will not necessarily run in a standalone Java client. For
example you can't use "@Inject JMSContext" or "@Resource ConnectionFactory" or @MessageDriven in a standalone Java client.

>
> I tried to learn what EJB container and Java EE web was, but it's completely obscure to me.

That's Java EE jargon for any code that is executed within the GlassFish server's JVM (rather than within a JVM that you
start).

>
> So I have the following options :
> - rewrite my code so that I don't use setMessageListener and setClientID, but I honestly don't know how I could do that
> - find another way to embedd a Glassfish server so that I can run my tests. Here as well, I don't know where to go.
>
> If someone could help me find the right direction, it would be really helpful, because I'm running out of ideas here.

Before I can answer that, I'd like to understand why you are running your code in the server at all, given that you're
not interested in "Message Driven Beans, Enterprise Java Beans, and Web applications".

You say you want to "send and receive messages between my java applications". If these are ordinary Java applications
that don't run in GlassFish (i.e. you run then directly using the java command) then it sounds as if all you are using
GlassFish for is as a JMS server. Is that right?

Nigel