データベースから個人 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 への最初のルックアップも正常に機能しますが、2 回目はエラー メッセージでクラッシュします
[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 はどうなりますか? いいえ、再初期化または再作成する必要がありますか?