我一直在关注this tutorial了解 JMS 和 Glassfish 的工作原理。我没有使用 Netbeans,而是使用 Eclipse (Juno),并通过命令行成功运行了应用程序(Producer、Synchconsumer、AsynchConsumer 等):生产者的 appclient -client nameOFJarFile typeOfMessage numberOfMessages
和消费者的 appclient -client nameOFJarFile typeOfMessage
。
我试图让这一切在 Eclipse 中工作,以便我能够逐步执行代码以查看事情如何更好地工作(因为我希望必须为我将构建的 JMS 应用程序执行此操作),但我无法让这些教程文件“正确”显示并在 Eclipse 中运行。
通过正确显示,我的意思是:我已将 simple
父项目导入到 Eclipse 中,并导航到并打开了 .java 文件。我设置 IDE 的方式,每个保留字/变量/其他所有内容都应该以不同的颜色显示:
它的显示方式仅显示保留字和字符串,告诉我有些问题,但我不确定是什么:
我在 Eclipse 中运行 Glassfish 服务器,但当我单击“运行”按钮并转到“运行方式”时,没有选项可以在 Glassfish 服务器上运行该文件。相反,默认选项是将父项目作为 Maven 构建运行simple
:
如何将这些应用程序配置为“正确”显示并在 Eclipse IDE 的控制台中运行,就像我输入了 appclient -clientsynchconsumer.jarqueue
一样?
最佳答案
所以你有几个问题。一大一小。
首先是大的。本教程示例是一个应用程序客户端。所以你能做的就是创建一个独立的。首先创建一个新的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>
这些依赖项有很多子依赖项,因此下载可能需要一段时间。不过,为了 future 的发展,你会很高兴拥有它们。依赖关系也有利于 EJB 查找。正如您将在下面看到的,无需将 JNDI 名称传递给 lookup()
,只需传递 session bean 的完全限定类名即可。
现在是小问题了。本教程中的程序旨在从命令行运行。您可以进行一些简单的调整。只需复制该程序(我将从 Producer 类开始)并进行一些更改。
去掉@Resource
注释。相反,您将使用 javax.naming.InitialContext 来查找目录。最终的代码将如下所示(假设您创建了从命令行运行程序所需的管理对象,正如您所说)
我刚刚对 NUM_MSGS
和 desType
进行了硬编码。
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);
}
}
<小时/>
可信链接(帮助我解决与您相同的问题)
https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB
With which maven dependencies can i create a standalone JMS client for Glassfish?
祝你好运!
关于java - 如何在 Eclipse (Juno) 中的 Glassfish 4.0 上运行 JMS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23190846/