简体   繁体   English

如何在Eclipse(Juno)的Glassfish 4.0上运行JMS应用程序

[英]How to run a JMS application on Glassfish 4.0 within Eclipse (Juno)

I have been following this tutorial to understand how JMS & Glassfish works. 我一直在关注本教程,以了解JMS和Glassfish的工作方式。 Instead of using Netbeans, I have been using Eclipse (Juno) and have successfully ran the applications (Producer, Synchconsumer, AsynchConsumer, etc.) via command-line: appclient -client nameOFJarFile typeOfMessage numberOfMessages for the producer and appclient -client nameOFJarFile typeOfMessage for the consumers. 我没有使用Netbeans,而是一直使用Eclipse(Juno),并已通过命令行成功运行了应用程序(生产者,同步消费者,AsynchConsumer等):生产者的appclient -client nameOFJarFile typeOfMessage appclient -client nameOFJarFile typeOfMessage numberOfMessages以及生产者的appclient -client nameOFJarFile typeOfMessage消费者。

I am trying to get this all to work in Eclipse so that I am able to step through the code to see how things work better (as I expect to have to do this for the JMS application that I will be building), but I am unable to get these tutorial files to show up "properly" and run in Eclipse. 我试图使所有这些在Eclipse中都能正常工作,以便能够逐步执行代码以查看事情如何更好地工作(因为我希望必须为将要构建的JMS应用程序完成此工作)。无法使这些教程文件“正确”显示并在Eclipse中运行。

By show up properly, I mean: I have imported the simple parent project into Eclipse and have navigated to and opened the .java files. 通过正确显示,我的意思是:我已将simple父项目导入Eclipse,并导航到并打开了.java文件。 The way I have the IDE set up, each reserved word / variable / everything else is supposed to display in different colors: 我设置IDE的方式是,每个保留字/变量/其他所有内容都应以不同的颜色显示:

rainbowColors

The way it is displaying only shows reserved words and Strings, telling me something is wrong, but I'm not sure what: 它的显示方式仅显示保留字和字符串,告诉我有什么问题,但是我不确定是什么:

synchronousConsumerColors

I have the Glassfish server running in Eclipse, though when I click the Run button and go to Run As, there is no option to run the file on the Glassfish server. 我已经在Eclipse中运行了Glassfish服务器,但是当我单击“运行”按钮并转到“运行方式”时,没有选项可以在Glassfish服务器上运行文件。 Instead, the default option is to run the parent project simple as a Maven build: 相反,默认选项是作为Maven构建simple地运行父项目

RunAs_Nothing

How can I configure these applications to both show up "properly" and to run within the Eclipse IDE's console, just as if I had typed appclient -client synchconsumer.jar queue ? 如何配置这些应用程序以使其“正确”显示并在Eclipse IDE的控制台中运行,就像我键入了appclient -client synchconsumer.jar queue

So you have a couple problems. 所以你有几个问题。 A big one and a small one. 大一小。

Big one first. 大第一。 The tutorial example is an appclient. 本教程示例是一个appclient。 So what you can do is create a stand alone. 因此,您可以做的是创建一个独立的服务器。 First create a new Maven project. 首先创建一个新的Maven项目。 You will need to attach the javaee-api dependency 您将需要附加javaee-api依赖项

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
</dependency>

From there, you will need a few more dependencies to be able to use glassfish and jms for stand alone apps. 从那里开始,您将需要更多的依赖关系才能将glassfish和jms用于独立的应用程序。

<dependency>
  <groupId>org.glassfish.main.appclient</groupId>
  <artifactId>gf-client</artifactId>
  <version>4.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.mq</groupId>
    <artifactId>imqjmsra</artifactId>
    <version>5.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.mq</groupId>
    <artifactId>imqbroker</artifactId>
    <version>5.0</version>
</dependency>

These dependencies have a lot of subdepencies, so it may take a while to download. 这些依赖项有很多子依赖项,因此下载可能需要一段时间。 You'll be glad you have them though, for future development. 不过,您会为将来的发展而高兴地拥有它们。 The dependencies are good for EJB look up also. 依赖关系也有利于EJB查找。 Instead of passing the JNDI name to the lookup() , as you'll see below, just pass the fully qualified class name of the session bean. 无需将JNDI名称传递给lookup() ,正如您将在下面看到的那样,只需传递会话bean的完全限定的类名即可。

So now the Small problem. 所以现在是小问题。 The program from the tutorial is meant to be ran from the command line. 本教程中的程序旨在从命令行运行。 You can make some simple adjustments. 您可以进行一些简单的调整。 Just copy the program (I'll start you off with the Producer class) and make a few changes. 只需复制程序(我将以Producer类开始),然后进行一些更改。

Get rid of the @Resource annotations. 摆脱@Resource批注。 Instead you will use javax.naming.InitialContext to lookup the directory. 相反,您将使用javax.naming.InitialContext查找目录。 The final code will look like this (assuming you're created the admin objects required to run the program from the command line, as you said you have) 最终代码将如下所示(假设您已经创建了从命令行运行程序所需的管理对象,就像您说的那样)

I just hard coded the NUM_MSGS and the desType . 我只是对NUM_MSGSdesType了硬编码。

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSRuntimeException;
import javax.jms.Queue;
import javax.jms.Topic;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Producer {

    private static ConnectionFactory connectionFactory;
    private static Queue queue;
    private static Topic topic;

    public static void main(String[] args) throws NamingException {
        final int NUM_MSGS = 10;
        InitialContext ic = new InitialContext();
        connectionFactory = (ConnectionFactory)ic.lookup("java:comp/DefaultJMSConnectionFactory");
        queue = (Queue)ic.lookup("jms/MyQueue");
        topic = (Topic)ic.lookup("jms/MyTopic");

        String destType = "queue";
        System.out.println("Destination type is " + destType);

        if (!(destType.equals("queue") || destType.equals("topic"))) {
            System.err.println("Argument must be \"queue\" or " + "\"topic\"");
            System.exit(1);
        }

        Destination dest = null;

        try {
            if (destType.equals("queue")) {
                dest = (Destination) queue;
            } else {
                dest = (Destination) topic;
            }
        } catch (JMSRuntimeException e) {
            System.err.println("Error setting destination: " + e.toString());
            System.exit(1);
        }

        try (JMSContext context = connectionFactory.createContext();) {
            int count = 0;

            for (int i = 0; i < NUM_MSGS; i++) {
                String message = "This is message " + (i + 1) 
                        + " from producer";
                // Comment out the following line to send many messages
                System.out.println("Sending message: " + message);
                context.createProducer().send(dest, message);
                count += 1;
            }
            System.out.println("Text messages sent: " + count);


            context.createProducer().send(dest, context.createMessage());
            // Uncomment the following line if you are sending many messages
            // to two synchronous consumers
            // context.createProducer().send(dest, context.createMessage());
        } catch (JMSRuntimeException e) {
            System.err.println("Exception occurred: " + e.toString());
            System.exit(1);
        }
        System.exit(0);
    }
}

Credited Links (in helping me solve the same problem you have) 信誉链接(帮助我解决您遇到的相同问题)

GOOD LUCK! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM