向 JMS (Weblogic) 发送消息

Sending message to JMS (Weblogic)

当我运行以下代码时,消息似乎已发送到队列,但我在队列中看不到任何东西。没有错误,执行我的代码时出现异常。

我使用 Weblogic 服务器。

这是我的代码:

private InitialContext getInitialContext() throws NamingException {
    Hashtable env = new Hashtable();
    env.put(InitialContext.INITIAL_CONTEXT_FACTORY, contextFactory);
    env.put(InitialContext.PROVIDER_URL, providerUrl);
    env.put(Context.SECURITY_PRINCIPAL, username);
    env.put(Context.SECURITY_CREDENTIALS, password);
    return new InitialContext(env);
}

public ConnectionFactory getConnectionFactory(InitialContext context) throws NamingException {
    return (ConnectionFactory) context.lookup(ConnectionParameter.JMS_CONNECTION_FACTORY_JNDI);
}

public void send() throws NamingException, JMSException {
    InitialContext context = getInitialContext();
    Destination destination = (Destination) context.lookup("jms/dpdr/mhcinterface/arnoldQueue");

    try (Connection connection = getConnectionFactory(context).createConnection();){
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(destination);
        Message message = session.createTextMessage("work order complete!");
        sender.send(message);
        session.commit();
        session.close();
    }
    context.close();

    System.out.println("-- end --");
}

知道这里有什么问题吗?

您似乎忘记在发送消息前调用 connection.start()。您可以像下面这样操作:

MessageProducer sender = session.createProducer(destination);
connection.start();
Message message = session.createTextMessage("work order complete!");