gpt4 book ai didi

glassfish - 在 glassfish 服务器消息队列上发送消息

转载 作者:行者123 更新时间:2023-12-05 08:01:30 29 4
gpt4 key购买 nike

我只是测试在 glassfish 服务器队列上发送消息,因此我在 glassfish 服务器上创建了 JMSResource,如下所示:

enter image description here

现在我已经在 Netbeans IDE 中创建了一个 Java 应用程序。这是我的 Java 文件:-

import javax.annotation.Resource;  
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

public class MessageSender
{
@Resource(mappedName = "jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName = "jms/Queue")
private static Queue queue;

public void produceMessages()
{
MessageProducer messageProducer;
TextMessage textMessage;
try
{
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
messageProducer = session.createProducer(queue);
textMessage = session.createTextMessage();

textMessage.setText("Testing, 1, 2, 3. Can you hear me?");
System.out.println("Sending the following message: "
+ textMessage.getText());
messageProducer.send(textMessage);

messageProducer.close();
session.close();
connection.close();
}
catch (JMSException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new MessageSender().produceMessages();
}
}

现在,当我运行这个简单的应用程序时,它在创建 ConnectionFactory 时出错:

Exception in thread "main" java.lang.NullPointerException
at simplesendamq.MessageSender.produceMessages(MessageSender.java:33)
at simplesendamq.MessageSender.main(MessageSender.java:55)

最佳答案

GF 客户端必须使用 JNDI 查找来获取连接工厂和消息目的地(主题/队列)。

因此,您必须形成 JNDI 上下文(在其中设置主机、ORB 端口和其他魔术参数)并在类路径中包含某些库(它需要与 GF 一起工作)。

我的例子:

Receiver.java

    import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;

public class Receiver implements MessageListener {
public static void main(String[] args) throws JMSException, NamingException {
try {
Properties props = new Properties();

props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

// Gets the JNDI context
Context jndiContext = new InitialContext(props);

// Looks up the administered objects
ConnectionFactory connectionFactory = (ConnectionFactory)
jndiContext.lookup("my.connection.factory");

Queue queue = (Queue) jndiContext.lookup("my.test.queue");

// Creates the needed artifacts to connect to the queue
Connection connection = connectionFactory.createConnection();

Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

MessageConsumer consumer = session.createConsumer(queue);

connection.start();

} catch(Exception e) {
e.printStackTrace();
}
}

@Override
public void onMessage(Message message) {
System.out.println("onMsg!");
}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>examples.my</groupId>
<artifactId>jms-test</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>com.sun.messaging</groupId>
<artifactId>imqjmsra</artifactId>
<version>4.4</version>
</dependency>

<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms-api</artifactId>
<version>1.1-rev-1</version>
</dependency>

<dependency>
<groupId>org.glassfish.common</groupId>
<artifactId>common-util</artifactId>
<version>3.0</version>
</dependency>

<dependency>
<groupId>org.glassfish.appclient</groupId>
<artifactId>gf-client</artifactId>
<version>3.0-b74b</version>
</dependency>

<dependency>
<groupId>org.glassfish.appclient.server</groupId>
<artifactId>appclient-server-core</artifactId>
<version>3.0-b74b</version>
</dependency>

<dependency>
<groupId>com.sun.corba</groupId>
<artifactId>glassfish-corba-orb</artifactId>
<version>3.0.1-b001</version>
</dependency>

<dependency>
<groupId>org.glassfish.security</groupId>
<artifactId>security</artifactId>
<version>3.0-b74b</version>
</dependency>
</dependencies>

</project>

关于glassfish - 在 glassfish 服务器消息队列上发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13641051/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com