簡體   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