Switching from NetBeans to IntelliJ, questions about using J2EE JMS using Glassfish

I'm recently switching from NetBeans 6.9.1, and using Glassfish 3.0.1.  I have a very small JMS test application that I can't get to work with IntelliJ.  The issues are the following:
1) none of the javax.jms classes can be found.  In NetBeans, when I select Glassfish as the application server, it appears that all of the J2EE classes are imported as well.  How can I add the J2EE classes to the IntelliJ classpath for my application?
2) Again, in NetBeans, I have an option to create a new JMS Resource using a wizard that adds the ConnectionFactory objects and Queue objects to the sun-resources.xml file for me.  Is there a way to do this in IntelliJ as well or is this a manual task that needs to be completed in IntelliJ?
3) Also, in the code listed below, when the javax.annotation.Resource class is imported, the lookup token is still unrecognized.  Is that a defect in IntelliJ or again, am I doing something incorrect that I should be doing differently?

The code posted below compiles and executes fine in NetBeans, I'm trying to duplicate this same project in IntelliJ.  Any help would be appreciated.  Thanks!

package applicationclient1;

import java.util.logging.Logger;
import java.util.logging.Level;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

public class Main {
    @Resource(lookup = "jms/myQueue") private static Queue queue;
    @Resource(lookup = "jms/myConnection") private static ConnectionFactory connectionFactory;
    private static final Logger logger = Logger.getLogger(Main.class.getName());

    public static void main(String[] args) {
        Main me = new Main();
        logger.log(Level.INFO, "Main started!");

        if (args[0].equalsIgnoreCase("send")) {
            logger.log(Level.INFO, "Sending a message");
            me.SendMessage("This is a test message at " + String.valueOf(Thread.currentThread().getId()));
        } else if (args[0].equalsIgnoreCase("receive")) {
            logger.log(Level.INFO, "Receiving a message");
           me.ReceiveMessage();
        }

        logger.log(Level.INFO, "Main completed!");
        System.exit(0);
    }

    private void SendMessage(String Message) {
        try {
            logger.log(Level.INFO, "SendMessage starting...");
            Connection connection = connectionFactory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(queue);
            TextMessage textMessage = session.createTextMessage(Message);
            producer.send(textMessage);
            connection.close();
            logger.log(Level.INFO, "Message sent!");
        } catch (JMSException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }

    private void ReceiveMessage() {
        logger.log(Level.INFO, "ReceiveMessage starting");
        throw new IllegalArgumentException("Method not implemented!");
    }
}
6 comments
Comment actions Permalink

Have set up an Application Server for Glassfish?

File->Settings->Application Servers

Then select Add, select Glassfish and point IntelliJ at the top-level Glassfish directory. Please note that this assumes that the Glassfish plugin is installed and enabled; however, that plugin is bundled with IntelliJ so as long as you haven't disabled it you should be good to go.

Then you need to add that Application Server as a dependency to your application.

File->Project Structure->Select Modules->Select your web module (may be the top-level module depending on your setup)->Select Dependencies Tab->Select Add Button->Select Application Server Library

Your application server you setup should be in the list. Select it and all of its jars should then be in the classpath for your project.

0
Comment actions Permalink

I had done that but the annotations were still unrecognized.  In fact, so were all of the import javax.jms.* lines.  I perused through Netbeans and found the classes that it has registed for Glassfish and have since added those to the application server.  So now when I look at my Glassfish Application Server property window, I now have a long list of classes that are listed in the libraries group.  The annotations are still not recognized and I've found that J2SE also includes a java.annotations class which is version 1.0.  Glassfish includes the newer 1.1 version.  So the resolution that I've found is that I need to move the Glassfish libraries higher in the classpath.  Easy enough.  I've opened the project structure, selected Modules, selected the Dependencies tab, and moved the Glassfish Server before my JDK.  GREAT!  Now the annotations work, and the application compiles!

It doesn't 'run' however.  1) Glassfish never starts when I hit run and 2) The application doesn't get deployed.  I presume this is because I know nothing about creating artifacts possibly.

It is just an enterprise application, no web interface.  I'm using Glassfish as the JMS container for the connections and queues.

0
Comment actions Permalink

hi!
I`m currently experiencing the same problem. Please help us to find a solution :)
Thanks in advance!

0
Comment actions Permalink

Are you trying to deploy your app as a war file? If so you are going to have to create a Glassfish Run/Debug Configuration.

0
Comment actions Permalink

I just want to report, that NetBeans are generating sun-resources.xml file that register JNDI names for Java Messaging Service (JMS). Idea doesn`t do such a thing, so you have to register all message services by yourself in Glassfish administration console. Anyway, I didn`t manage to configure it properly today.. still JNDI names are missing during glassfish deployment.

0
Comment actions Permalink

Xanothos wrote:


3) Also, in the code listed below, when the javax.annotation.Resource class is imported, the lookup token is still unrecognized.  Is that a defect in IntelliJ or again, am I doing something incorrect that I should be doing differently?

package applicationclient1;

import java.util.logging.Logger;
import java.util.logging.Level;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

public class Main {
    @Resource(lookup = "jms/myQueue") private static Queue queue;
    @Resource(lookup = "jms/myConnection") private static ConnectionFactory connectionFactory;
    private static final Logger logger = Logger.getLogger(Main.class.getName());
...


See this yourtrack issue for the @Resource(lookup="") compilation error http://youtrack.jetbrains.net/issue/IDEA-70927

0

Please sign in to leave a comment.