我有一条从数据库中获取一组人员 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 放入消息正文中
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 会发生什么?不,我需要重新初始化或重新创建还是什么?