Mule - 在jndi上存储数据源

时间:2013-09-27 09:58:46

标签: spring jndi mule

我正在尝试按照link1中的说明进行操作 和link2将数据源存储在jndi中,以便每个mule应用程序都可以访问它而无需重新定义它。 我得到了@David Dossot的帮助,并按照@Spina的建议制作了这个bean:

import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.jndi.JndiTemplate;


public class JndiExporter implements InitializingBean {

    private final JndiTemplate template = new JndiTemplate();

    private Map<String, Object> jndiMapping = null;

    @Override
    public void afterPropertiesSet() throws Exception {
            for(Entry<String, Object> addToJndi: jndiMapping.entrySet()){
                    template.bind(addToJndi.getKey(), addToJndi.getValue());
            }
    }

    public void setJndiMapping(Map<String, Object> jndiMapping) {
            this.jndiMapping = jndiMapping;
    }

}

然后是一个简单的骡子应用程序:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:context="http://www.springframework.org/schema/context" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc"
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/jdbc http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">    
    <jdbc-ee:oracle-data-source name="Oracle_Data_Source" user="user" password="pass" url="jdbc:oracle:thin:@xxxxxx" doc:name="Oracle Data Source" transactionIsolation="UNSPECIFIED"/>
    <jdbc-ee:connector name="jdbcConnectorTest" dataSource-ref="Oracle_Data_Source" validateConnections="true" queryTimeout="-1" doc:name="Database" pollingFrequency="0">
    </jdbc-ee:connector> 
     <spring:beans>
        <spring:bean id="jndiExportera" class="JndiExporter" name="jndiExporterBean">
            <spring:property name="jndiMapping">
                <spring:map>
                    <spring:entry key="jdbcConnectorTest" value="jdbcConnectorTest" />            
                </spring:map>
            </spring:property>
        </spring:bean>
    </spring:beans>
    <flow name="jndiConnectionFlow1" doc:name="jndiConnectionFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <logger level="INFO" doc:name="Logger"/>
    </flow> 
</mule>

不幸的是它不起作用,它在启动时出现以下错误:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.bind(Unknown Source)
    at org.springframework.jndi.JndiTemplate$2.doInContext(JndiTemplate.java:198)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    at org.springframework.jndi.JndiTemplate.bind(JndiTemplate.java:196)
    at JndiExporter.afterPropertiesSet(JndiExporter.java:18)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.mule.config.spring.SpringRegistry.doInitialise(SpringRegistry.java:89)
    at org.mule.registry.AbstractRegistry.initialise(AbstractRegistry.java:109)
    at org.mule.config.spring.SpringXmlConfigurationBuilder.createSpringRegistry(SpringXmlConfigurationBuilder.java:119)
    at org.mule.config.spring.SpringXmlConfigurationBuilder.doConfigure(SpringXmlConfigurationBuilder.java:73)
    at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:46)
    at org.mule.config.builders.AbstractResourceConfigurationBuilder.configure(AbstractResourceConfigurationBuilder.java:78)
    at org.mule.config.builders.AutoConfigurationBuilder.autoConfigure(AutoConfigurationBuilder.java:101)
    at org.mule.config.builders.AutoConfigurationBuilder.doConfigure(AutoConfigurationBuilder.java:57)
    at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:46)
    at org.mule.config.builders.AbstractResourceConfigurationBuilder.configure(AbstractResourceConfigurationBuilder.java:78)
    at org.mule.context.DefaultMuleContextFactory.createMuleContext(DefaultMuleContextFactory.java:84)
    at org.mule.module.launcher.application.DefaultMuleApplication.init(DefaultMuleApplication.java:207)
    at org.mule.module.launcher.application.ApplicationWrapper.init(ApplicationWrapper.java:64)
    at org.mule.module.launcher.DefaultMuleDeployer.deploy(DefaultMuleDeployer.java:47)
    at org.mule.tooling.server.application.ApplicationDeployer.main(ApplicationDeployer.java:127)
INFO  2013-09-27 11:53:22,774 [main] org.mule.module.launcher.application.DefaultMuleApplication: App 'jndiconnection' never started, nothing to dispose of
Exception in thread "main" org.mule.module.launcher.DeploymentInitException: NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.mule.module.launcher.application.DefaultMuleApplication.init(DefaultMuleApplication.java:219)
    at org.mule.module.launcher.application.ApplicationWrapper.init(ApplicationWrapper.java:64)
    at org.mule.module.launcher.DefaultMuleDeployer.deploy(DefaultMuleDeployer.java:47)
    at org.mule.tooling.server.application.ApplicationDeployer.main(ApplicationDeployer.java:127)
Caused by: org.mule.api.config.ConfigurationException: Error creating bean with name 'jndiExportera' defined in URL [file:/C:/Users/enrico.curiotto/MuleStudio/workspace/.mule/apps/jndiconnection/jndiConnection.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial (org.mule.api.lifecycle.InitialisationException) (org.mule.api.config.ConfigurationException)
    at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:52)
    at org.mule.config.builders.AbstractResourceConfigurationBuilder.configure(AbstractResourceConfigurationBuilder.java:78)
    at org.mule.context.DefaultMuleContextFactory.createMuleContext(DefaultMuleContextFactory.java:84)
    at org.mule.module.launcher.application.DefaultMuleApplication.init(DefaultMuleApplication.java:207)
    ... 3 more
Caused by: org.mule.api.config.ConfigurationException: Error creating bean with name 'jndiExportera' defined in URL [file:/C:/Users/enrico.curiotto/MuleStudio/workspace/.mule/apps/jndiconnection/jndiConnection.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial (org.mule.api.lifecycle.InitialisationException)
    at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:52)
    at org.mule.config.builders.AbstractResourceConfigurationBuilder.configure(AbstractResourceConfigurationBuilder.java:78)
    at org.mule.config.builders.AutoConfigurationBuilder.autoConfigure(AutoConfigurationBuilder.java:101)
    at org.mule.config.builders.AutoConfigurationBuilder.doConfigure(AutoConfigurationBuilder.java:57)
    at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:46)
    ... 6 more
Caused by: org.mule.api.lifecycle.InitialisationException: Error creating bean with name 'jndiExportera' defined in URL [file:/C:/Users/enrico.curiotto/MuleStudio/workspace/.mule/apps/jndiconnection/jndiConnection.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.mule.registry.AbstractRegistry.initialise(AbstractRegistry.java:117)
    at org.mule.config.spring.SpringXmlConfigurationBuilder.createSpringRegistry(SpringXmlConfigurationBuilder.java:119)
    at org.mule.config.spring.SpringXmlConfigurationBuilder.doConfigure(SpringXmlConfigurationBuilder.java:73)
    at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:46)
    ... 10 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jndiExportera' defined in URL [file:/C:/Users/enrico.curiotto/MuleStudio/workspace/.mule/apps/jndiconnection/jndiConnection.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.mule.config.spring.SpringRegistry.doInitialise(SpringRegistry.java:89)
    at org.mule.registry.AbstractRegistry.initialise(AbstractRegistry.java:109)
    ... 13 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.bind(Unknown Source)
    at org.springframework.jndi.JndiTemplate$2.doInContext(JndiTemplate.java:198)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    at org.springframework.jndi.JndiTemplate.bind(JndiTemplate.java:196)
    at JndiExporter.afterPropertiesSet(JndiExporter.java:18)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    ... 24 more

有什么想法吗? 谢谢!

°°°°°°°°°°°°°°°°°°°°

更新

°°°°°°°°°°°°°°°°°°°°

@Genjosanzo回答我的春豆成了:

<spring:beans>  
    <spring:bean id="systemJndiPrereqs"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <spring:property name="targetObject" value="#{@systemProperties}" />
        <spring:property name="targetMethod" value="putAll" />
        <spring:property name="arguments">
            <!-- The new Properties -->
            <util:properties>
                <spring:prop key="#{ T(javax.naming.Context).URL_PKG_PREFIXES }">org.apache.naming</spring:prop>
            </util:properties>
        </spring:property>
    </spring:bean>  
    <spring:bean id="jndiExportera" name="jndiExporterBean"
        class="JndiExporter">
        <spring:property name="jndiMapping">
            <spring:map>
                <spring:entry key="jdbcConnectorTest" value="jdbcConnectorTest" />
            </spring:map>
        </spring:property>
    </spring:bean>
</spring:beans>

但不幸的是,我得到的错误与之前相同。

1 个答案:

答案 0 :(得分:1)

您获得的错误意味着您必须设置系统属性以启用jndiContext。

如果您想在弹簧配置中执行此操作,则应添加如下所示的bean

<bean id="systemJndiPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
      <property name="targetObject" value="#{@systemProperties}" />
      <property name="targetMethod" value="putAll" />
      <property name="arguments">
        <!-- The new Properties -->
        <util:properties>
          <prop key="#{ T(javax.naming.Context).URL_PKG_PREFIXES }">org.apache.naming</prop>
        </util:properties>
      </property>
  </bean>