有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ApacheCamel中的java NoInitialContextException

我有一个从数据库中获取一组个人id的路由,将它们拆分,并对每个id进行LDAP查找。下面的代码简化了,但我希望您能理解

主要

public class MainApp {
    public static void main(String... args) throws Exception {
        Main main = new Main();
        main.enableHangupSupport();
        // create mydb and bind it into registry
        DataSource dataSource = setupDataSource();
        main.bind("mydb", dataSource);
        // create ldap and bind it into registry
        InitialLdapContext ldap = setupLDAP();
        main.bind("dir", ldap);
        main.addRouteBuilder(new MyRouteBuilder());
        main.run(args);
    }

    private static InitialLdapContext setupLDAP() throws NamingException {
        Properties props = new Properties();
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");
        props.setProperty(Context.PROVIDER_URL, "ldap://mycompany:389");
        props.setProperty(Context.URL_PKG_PREFIXES, "com.sun.jndi.url");
        props.setProperty(Context.REFERRAL, "ignore");
        props.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
        return new InitialLdapContext(props, null);
    }
}

MyRouteBuilder

public class MyRouteBuilder extends RouteBuilder {
    public void configure() throws Exception {
        from("timer:start?period=0&delay=0&repeatCount=1")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                System.out.println("Invoked timer at " + new Date());
            }
        })
        .setBody(constant("select personid from person"))
        .to("jdbc:mydb?outputType=StreamList")
        .split(body())
        .process(new AfterDbProcessor())
        .to("ldap:dir?base=ou=people,dc=mycompany&returnedAttributes=sn,fn")
        .process(new AfterLDAPProcessor());
     }
}

AfterDbProcessor只需获取personid并将其放入in消息的正文中

public class AfterHOTProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {
        Map<String, Object> row = exchange.getIn().getBody(Map.class);
        Integer personId = (Integer) row.get("henkilo_id");
        exchange.getIn().setBody("(uid=" + personId + ")");
   }

}

从数据库获取ID工作正常。第一次查找到LDAP也可以,但第二次将崩溃并显示错误消息

[l-1) thread #0 - timer://start] TimerConsumer WARN  Error processing exchange.
Exchange[Message: org.apache.camel.component.jdbc.ResultSetIterator@65ce3f76]. 
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]

在第一次LDAP查找之后,InitialLdapContext会发生什么情况?不,我需要重新初始化或重新创建什么


共 (0) 个答案