Java 如何在Eclipse(Juno)中的Glassfish 4.0上运行JMS应用程序
我一直在关注JMS和Glassfish的工作原理。我没有使用Netbeans,而是使用Eclipse(Juno),并通过命令行成功运行了应用程序(Producer、Synchconsumer、AsynchConsumer等):Java 如何在Eclipse(Juno)中的Glassfish 4.0上运行JMS应用程序,java,eclipse,maven,glassfish,jms,Java,Eclipse,Maven,Glassfish,Jms,我一直在关注JMS和Glassfish的工作原理。我没有使用Netbeans,而是使用Eclipse(Juno),并通过命令行成功运行了应用程序(Producer、Synchconsumer、AsynchConsumer等):appclient-client-nameOFJarFile-typeOfMessage numberOfMessages用于生产者,而appclient-client-nameOFJarFile-TypeOfMessages用于消费者 我正试图在Eclipse中实现这一切
appclient-client-nameOFJarFile-typeOfMessage numberOfMessages
用于生产者,而appclient-client-nameOFJarFile-TypeOfMessages
用于消费者
我正试图在Eclipse中实现这一切,以便我能够逐步完成代码以了解如何更好地工作(因为我希望我将要构建的JMS应用程序必须这样做),但我无法让这些教程文件“正确”地显示并在Eclipse中运行
通过正确显示,我的意思是:我已经将simple
父项目导入到Eclipse中,并且已经导航到并打开了.java文件。按照我设置IDE的方式,每个保留字/变量/所有其他内容都应该以不同的颜色显示:
它的显示方式只显示保留字和字符串,告诉我有什么问题,但我不确定是什么:
我让Glassfish服务器在Eclipse中运行,但是当我单击Run按钮并转到RunAs时,没有在Glassfish服务器上运行该文件的选项。相反,默认选项是将父项目simple
作为Maven构建运行:
如何将这些应用程序配置为“正确”显示并在Eclipse IDE的控制台中运行,就像我键入了
appclient-client synchconsumer.jar queue
?一样,因此您有几个问题。一个大的和一个小的
第一个大的。本教程的示例是一个appclient。所以你能做的就是创建一个独立的。首先创建一个新的Maven项目。您需要附加javaeeapi
依赖项
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
信用链接(帮助我解决您遇到的相同问题)
<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>
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);
}
}