繁体   English   中英

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

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

我一直在关注本教程,以了解JMS和Glassfish的工作方式。 我没有使用Netbeans,而是一直使用Eclipse(Juno),并已通过命令行成功运行了应用程序(生产者,同步消费者,AsynchConsumer等):生产者的appclient -client nameOFJarFile typeOfMessage appclient -client nameOFJarFile typeOfMessage numberOfMessages以及生产者的appclient -client nameOFJarFile typeOfMessage消费者。

我试图使所有这些在Eclipse中都能正常工作,以便能够逐步执行代码以查看事情如何更好地工作(因为我希望必须为将要构建的JMS应用程序完成此工作)。无法使这些教程文件“正确”显示并在Eclipse中运行。

通过正确显示,我的意思是:我已将simple父项目导入Eclipse,并导航到并打开了.java文件。 我设置IDE的方式是,每个保留字/变量/其他所有内容都应以不同的颜色显示:

rainbowColors

它的显示方式仅显示保留字和字符串,告诉我有什么问题,但是我不确定是什么:

synchronousConsumerColors

我已经在Eclipse中运行了Glassfish服务器,但是当我单击“运行”按钮并转到“运行方式”时,没有选项可以在Glassfish服务器上运行文件。 相反,默认选项是作为Maven构建simple地运行父项目

RunAs_Nothing

如何配置这些应用程序以使其“正确”显示并在Eclipse IDE的控制台中运行,就像我键入了appclient -client synchconsumer.jar queue

所以你有几个问题。 大一小。

大第一。 本教程示例是一个appclient。 因此,您可以做的是创建一个独立的服务器。 首先创建一个新的Maven项目。 您将需要附加javaee-api依赖项

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

从那里开始,您将需要更多的依赖关系才能将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>

这些依赖项有很多子依赖项,因此下载可能需要一段时间。 不过,您会为将来的发展而高兴地拥有它们。 依赖关系也有利于EJB查找。 无需将JNDI名称传递给lookup() ,正如您将在下面看到的那样,只需传递会话bean的完全限定的类名即可。

所以现在是小问题。 本教程中的程序旨在从命令行运行。 您可以进行一些简单的调整。 只需复制程序(我将以Producer类开始),然后进行一些更改。

摆脱@Resource批注。 相反,您将使用javax.naming.InitialContext查找目录。 最终代码将如下所示(假设您已经创建了从命令行运行程序所需的管理对象,就像您说的那样)

我只是对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);
    }
}

信誉链接(帮助我解决您遇到的相同问题)

祝好运!

暂无
暂无

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

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