javax.naming.NameNotFoundException:与Wildfly 9.0.1.Final

时间:2015-09-09 07:26:26

标签: java jboss jms jndi wildfly

我正在Wildfly 9.0.1.Final上部署基于struts的应用程序。我正在尝试加载ConnectionFactory,但有些原因它不起作用。

我尝试获取ConnectionFactory的代码:

public void init(String jndiConnectionFactory, String jndiQueue, boolean autoAcknowledge) {
        try {
            context = new InitialContext(); 
            ServiceLocator sl = ServiceLocator.getInstance();
            queueConnectionFactory = sl.getQueueConnectionFactory(jndiConnectionFactory);
            queueConnection = queueConnectionFactory.createQueueConnection();
            queueSession = queueConnection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
            Queue queue = sl.getQueue(jndiQueue);
            queueSender = queueSession.createSender(queue);
        } catch (Exception ex) {
            logger.error("Naming Exception in JMSClient Constructor", ex);
        }
    } 

方法的参数如下:

String jndiConnectionFactory = "ConnectionFactory"
String jndiQueue = "queue/AuditMDB"

例外:

javax.naming.NameNotFoundException: ConnectionFactory -- service jboss.naming.context.java.ConnectionFactory
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
    at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:235)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at com.aldorsolutions.webfdms.util.ServiceLocator.getQueueConnectionFactory(ServiceLocator.java:101)
    at com.aldorsolutions.webfdms.util.JMSClient.init(JMSClient.java:51)
    at com.aldorsolutions.webfdms.audit.client.AuditClient.record(AuditClient.java:39)
    at com.aldorsolutions.webfdms.util.DAO.updateAudit(DAO.java:200)
    at com.aldorsolutions.webfdms.util.DAO.insertAudit(DAO.java:204)
    at com.aldorsolutions.webfdms.util.LoginLogger.logUser(LoginLogger.java:75)
    at com.aldorsolutions.webfdms.util.LoginLogger.logUser(LoginLogger.java:46)
    at fdms.ui.struts.action.ProcessLogon.execute(ProcessLogon.java:102)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130)
    at fdms.ui.ActionTimmingFilter.doFilter(ActionTimmingFilter.java:66)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
    at fdms.ui.SessionFilter.doFilter(SessionFilter.java:94)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:56)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

我的standalone-full.xml文件:

<subsystem xmlns="urn:jboss:domain:messaging:3.0">
            <hornetq-server>
                <journal-file-size>102400</journal-file-size>
                <connectors>
                    <http-connector name="http-connector" socket-binding="http">
                        <param key="http-upgrade-endpoint" value="http-acceptor"/>
                    </http-connector>
                    <http-connector name="http-connector-throughput" socket-binding="http">
                        <param key="http-upgrade-endpoint" value="http-acceptor-throughput"/>
                        <param key="batch-delay" value="50"/>
                    </http-connector>
                    <in-vm-connector name="in-vm" server-id="0"/>
                </connectors>
                <acceptors>
                    <http-acceptor name="http-acceptor" http-listener="default"/>
                    <http-acceptor name="http-acceptor-throughput" http-listener="default">
                        <param key="batch-delay" value="50"/>
                        <param key="direct-deliver" value="false"/>
                    </http-acceptor>
                    <in-vm-acceptor name="in-vm" server-id="0"/>
                </acceptors>
                <security-settings>
                    <security-setting match="#">
                        <permission type="send" roles="guest"/>
                        <permission type="consume" roles="guest"/>
                        <permission type="createNonDurableQueue" roles="guest"/>
                        <permission type="deleteNonDurableQueue" roles="guest"/>
                    </security-setting>
                </security-settings>
                <address-settings>
                    <!--default for catch all-->
                    <address-setting match="#">
                        <dead-letter-address>jms.queue.DLQ</dead-letter-address>
                        <expiry-address>jms.queue.ExpiryQueue</expiry-address>
                        <max-size-bytes>10485760</max-size-bytes>
                        <page-size-bytes>2097152</page-size-bytes>
                        <message-counter-history-day-limit>10</message-counter-history-day-limit>
                    </address-setting>
                </address-settings>
                <jms-connection-factories>
                    <connection-factory name="InVmConnectionFactory">
                        <connectors>
                            <connector-ref connector-name="in-vm"/>
                        </connectors>
                        <entries>
                            <entry name="java:/ConnectionFactory"/>
                        </entries>
                    </connection-factory>
                    <connection-factory name="RemoteConnectionFactory">
                        <connectors>
                            <connector-ref connector-name="http-connector"/>
                        </connectors>
                        <entries>
                            <entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
                        </entries>
                    </connection-factory>
                    <pooled-connection-factory name="hornetq-ra">
                        <transaction mode="xa"/>
                        <connectors>
                            <connector-ref connector-name="in-vm"/>
                        </connectors>
                        <entries>
                            <entry name="java:/JmsXA"/>
                            <!-- Global JNDI entry used to provide a default JMS Connection factory to EE application -->
                            <entry name="java:jboss/DefaultJMSConnectionFactory"/>
                        </entries>
                    </pooled-connection-factory>
                </jms-connection-factories>
                <jms-destinations>
                    <jms-queue name="ExpiryQueue">
                        <entry name="java:/jms/queue/ExpiryQueue"/>
                    </jms-queue>
                    <jms-queue name="DLQ">
                        <entry name="java:/jms/queue/DLQ"/>
                    </jms-queue>
                </jms-destinations>
            </hornetq-server>
        </subsystem>

服务定位器代码:

    package com.aldorsolutions.webfdms.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.EJBHome;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import org.apache.log4j.Logger;

public class ServiceLocator {

    private Map cache = null;

    static private Logger logger = Logger.getLogger(ServiceLocator.class
            .getName());

    static private ServiceLocator serviceLocator = null;

    private InitialContext ctx = null;
    private DataSourceManager dsMgr = null;

    /**
     * Create static ServiceLocator object upon initialization
     */
    static {

        try {
            serviceLocator = new ServiceLocator();
        } catch (Exception e) {
            logger.error("Error in initializing ServiceLocator : ", e);
        }
    }

    /**
     * Constructor to create ServiceLocator
     * 
     */
    private ServiceLocator() throws Exception {
        ctx = new InitialContext();
        cache = Collections.synchronizedMap(new HashMap());
        dsMgr = new DataSourceManager();
    }

    /**
     * 
     * @return retrieve instance of static ServiceLocator
     */
    static public ServiceLocator getInstance() {
        return serviceLocator;
    }

    /**
     * Retrieve an EJBLocalHome object
     * 
     * @param serviceId
     * @return
     * @throws Exception
     */
    public EJBHome getEJBHome(String jndiName, Class className)
            throws Exception {

        EJBHome ejbHome = null;

        if (cache.containsKey(jndiName)) {
            ejbHome = (EJBHome) cache.get(jndiName);
        } else {
            logger.debug("Looking up service : " + jndiName);
            Object obj = ctx.lookup(jndiName);
            Object portObj = PortableRemoteObject.narrow(obj, className);
            ejbHome = (EJBHome) portObj;
            cache.put(jndiName, ejbHome);
        }
        return ejbHome;
    }

    /**
     * Caches the QueueConnectionFactory in memory.
     * 
     * @param jndiName
     *            The jndi name of the QueueConnectionFactory
     * @return The newly looked up or cached QueueConnectionFactory
     * @throws LayeredTechException
     */
    public QueueConnectionFactory getQueueConnectionFactory(String jndiName)
            throws Exception {

        QueueConnectionFactory queueConnectionFactory = null;
        if (cache.containsKey(jndiName)) {
            queueConnectionFactory = (QueueConnectionFactory) cache
                    .get(jndiName);
        } else {
            logger.info("Looking up service : " + jndiName);
            queueConnectionFactory = (QueueConnectionFactory) ctx
                    .lookup(jndiName);
            cache.put(jndiName, queueConnectionFactory);
        }
        return queueConnectionFactory;
    }

    /**
     * Caches Queue in memory.
     * 
     * @param jndiName
     *            The jndi name of the Queue
     * @return The newly looked up or cached Queue
     * @throws LayeredTechException
     */
    public Queue getQueue(String jndiName) throws Exception {

        Queue queue = null;
        if (cache.containsKey(jndiName)) {
            queue = (Queue) cache.get(jndiName);
        } else {
            logger.debug("Looking up service : " + jndiName);
            queue = (Queue) ctx.lookup(jndiName);
            cache.put(jndiName, queue);
        }
        return queue;
    }

    public Connection getConnectionFromCache(String jndiLookup) throws SQLException {
        return ( dsMgr.createConnection(jndiLookup) );
    }

}

1 个答案:

答案 0 :(得分:0)

我已成功测试并运行,下面是我的整个代码段

独立-full.xml

 <subsystem xmlns="urn:jboss:domain:messaging-activemq:2.0">
            <server name="default">
                <security-setting name="#">
                    <role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/>
                </security-setting>
                <address-setting name="#" dead-letter-address="jms.queue.DLQ" expiry-address="jms.queue.ExpiryQueue" max-size-bytes="10485760" page-size-bytes="2097152" message-counter-history-day-limit="10"/>
                <http-connector name="http-connector" socket-binding="http" endpoint="http-acceptor"/>
                <http-connector name="http-connector-throughput" socket-binding="http" endpoint="http-acceptor-throughput">
                    <param name="batch-delay" value="50"/>
                </http-connector>
                <in-vm-connector name="in-vm" server-id="0">
                    <param name="buffer-pooling" value="false"/>
                </in-vm-connector>
                <http-acceptor name="http-acceptor" http-listener="default"/>
                <http-acceptor name="http-acceptor-throughput" http-listener="default">
                    <param name="batch-delay" value="50"/>
                    <param name="direct-deliver" value="false"/>
                </http-acceptor>
                <in-vm-acceptor name="in-vm" server-id="0">
                    <param name="buffer-pooling" value="false"/>
                </in-vm-acceptor>
                <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
                <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
                <jms-queue name="DeckQueue" entries="java:/jboss/exported/jms/queue/DeckQueue"/>
                <connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
                <connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
                <pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/>
            </server>
        </subsystem>

和JAVA代码

javax.naming.Context.lookup("java:jboss/DefaultJMSConnectionFactory");
--
JmsConnectionFactory jmsConnectionFactory.lookup("java:jboss/DefaultJMSConnectionFactory");
--
@MessageDriven(activationConfig = {
            @ActivationConfigProperty(
            propertyName = "destinationType", propertyValue ="javax.jms.Queue"),
    @ActivationConfigProperty(
                    propertyName = "destination", propertyValue = "java:/jboss/exported/jms/queue/DeckQueue")})
public class JmsMessageBean implements MessageListener {
--