如何使JNDI Registry线程安全?

时间:2018-10-08 06:45:17

标签: java apache-camel jndi futuretask

我有一个可以动态创建骆驼路线的类。 以前它是通过一个简单的循环创建路由的,但是现在我要确保所有路由都同时启动。为此,我使用了FutureTask。 现在我有了例外,

java.lang.UnsupportedOperationException: JBAS011859: Naming context is read-only

JNDI类中使用了一个CreateCamelRoute注册表。 每当尝试将对象绑定到该对象时,它都会引发异常。 如何使JNDI注册表线程安全?

public StartRoutes(){
           List<DestinationTransport> transport = findAll();

            FutureTask[] randomNumberTasks = new FutureTask[transport.size()];

            for (int i = 0; i < transport.size(); i++) {
                Callable callable = new CallableExampleDT(transport.get(i));

                randomNumberTasks[i] = new FutureTask(callable);

                Thread t = new Thread(randomNumberTasks[i]);
                t.start();
            }

            for (int i = 0; i < transport.size(); i++) {
                System.out.println(randomNumberTasks[i].get());
                messages.add((String) randomNumberTasks[i].get());
            }
        }
}

CallableExampleDT

public class CallableExampleDT implements Callable<Object> {
    private final Object transport;

    public CallableExampleDT(Object transport) {
       this.transport = transport;
    }

public synchronized Object call() throws Exception {

    String status = "";
    DestinationTransport t= (DestinationTransport) transport;
    try {
            route.createRoute(t);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Thread.sleep(5000);


    if (route.checkRoute(t.id()))
        status= LoggingMessage.STARTED;
    else
        status= LoggingMessage.IS_STOPPED;

    return status;
}

AsyncCamelDynamicFTPRoute

public class AsyncCamelDynamicFTPRoute{

    DefaultCamelContext defaultCamelContext = null;

    private CamelContext context = null;

    JndiRegistry jndi = new JndiRegistry();

    public synchronized void createRoute(Object object) throws ApplicationException {
        DestinationTransport dt= (DestinationTransport) object;
        String destinationTransport= dt.id();


        if(Objects.isNull(jndi.lookupByName("DYNAMIC_QUEUES_"+dt.getDestinationTransport()))){
            defaultCamelContext = new DefaultCamelContext();
            defaultCamelContext.setName("DYNAMIC_QUEUES_"+dt.getDestinationTransport());

            //getting exception here, while trying to bind
            jndi.bind("DYNAMIC_QUEUES_"+dt.getDestinationTransport(), defaultCamelContext);

        }
        else {

                defaultCamelContext = (DefaultCamelContext) jndi.lookup("DYNAMIC_QUEUES_"+dt.getDestinationTransport());

        }
        context= defaultCamelContext;

        context.getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "true");

            context.addRoutes(new RouteBuilder() {
                    /**Code for creating route**/
           }

      context.start();

 }

登录

ERROR [stderr] (Thread-89) java.lang.UnsupportedOperationException: JBAS011859: Naming context is read-only
01:26:57,536 ERROR [stderr] (Thread-89)     at org.jboss.as.naming.WritableServiceBasedNamingStore.requireOwner(WritableServiceBasedNamingStore.java:161)
01:26:57,536 ERROR [stderr] (Thread-89)     at org.jboss.as.naming.WritableServiceBasedNamingStore.bind(WritableServiceBasedNamingStore.java:66)
01:26:57,536 ERROR [stderr] (Thread-89)     at org.jboss.as.naming.NamingContext.bind(NamingContext.java:253)
01:26:57,537 ERROR [stderr] (Thread-89)     at org.jboss.as.naming.InitialContext$DefaultInitialContext.bind(InitialContext.java:260)
01:26:57,537 ERROR [stderr] (Thread-89)     at org.jboss.as.naming.NamingContext.bind(NamingContext.java:262)
01:26:57,537 ERROR [stderr] (Thread-89)     at javax.naming.InitialContext.bind(InitialContext.java:425)
01:26:57,537 ERROR [stderr] (Thread-89)     at javax.naming.InitialContext.bind(InitialContext.java:425)
01:26:57,537 ERROR [stderr] (Thread-89)     at org.apache.camel.impl.JndiRegistry.bind(JndiRegistry.java:124)
01:26:57,537 ERROR [stderr] (Thread-89)     at com.sial.integration.routing.AsyncCamelDynamicFTPRoute.createRoute(AsyncCamelDynamicFTPRoute.java:91)
01:26:57,537 ERROR [stderr] (Thread-89)     at com.sial.integration.routing.AsyncCamelDynamicFTPRoute$Proxy$_$$_WeldClientProxy.createRoute(Unknown Source)
01:26:57,537 ERROR [stderr] (Thread-89)     at com.sial.integration.api.CallableExampleDT.call(CallableExampleDT.java:27)
01:26:57,537 ERROR [stderr] (Thread-89)     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
01:26:57,537 ERROR [stderr] (Thread-89)     at java.lang.Thread.run(Thread.java:745)

0 个答案:

没有答案