向JMS发送消息(Weblogic)

时间:2017-07-21 08:24:41

标签: queue jms weblogic message-queue weblogic12c

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

我使用的是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 --");
}

请问这有什么问题吗?

1 个答案:

答案 0 :(得分:0)

您似乎忘记在发送邮件之前调用connection.start()。你可以这样做:

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