Search in sources :

Example 1 with QueueHandlerContainer

use of jp.ossc.nimbus.service.queue.QueueHandlerContainer in project nimbus by nimbus-org.

the class BeanFlowInvokerAccessImpl2 method invokeAsynchFlow.

public Object invokeAsynchFlow(Object obj, BeanFlowMonitor monitor, BeanFlowAsynchInvokeCallback callback, int maxAsynchWait) throws Exception {
    QueueHandlerContainer qhc = factoryCallBack.getAsynchInvokeQueueHandlerContainer();
    if (qhc == null) {
        throw new UnsupportedOperationException();
    }
    if (maxAsynchWait > 0 && qhc.size() > maxAsynchWait) {
        throw new UnavailableFlowException(flowName);
    }
    BeanFlowInvoker invoker = (BeanFlowInvoker) factoryCallBack.createFlow(flowName);
    BeanFlowMonitor newMonitor = createMonitor();
    if (callback != null) {
        ((BeanFlowMonitorImpl) monitor).addBeanFlowMonitor(newMonitor);
    }
    BeanFlowAsynchContext context = callback == null ? new BeanFlowAsynchContext(invoker, obj, newMonitor) : new BeanFlowAsynchContext(invoker, obj, newMonitor, callback);
    if (factoryCallBack.getThreadContext() != null) {
        context.putThreadContextAll(factoryCallBack.getThreadContext());
    }
    ((BeanFlowMonitorImpl) newMonitor).addAsynchContext(context);
    qhc.push(context);
    return context;
}
Also used : QueueHandlerContainer(jp.ossc.nimbus.service.queue.QueueHandlerContainer) BeanFlowAsynchContext(jp.ossc.nimbus.service.queue.BeanFlowAsynchContext)

Example 2 with QueueHandlerContainer

use of jp.ossc.nimbus.service.queue.QueueHandlerContainer in project nimbus by nimbus-org.

the class BeanFlowInvokerAccessImpl2 method invokeAsynchFlow.

public Object invokeAsynchFlow(Object obj, BeanFlowMonitor monitor, boolean isReply, int maxAsynchWait) throws Exception {
    QueueHandlerContainer qhc = factoryCallBack.getAsynchInvokeQueueHandlerContainer();
    if (qhc == null) {
        throw new UnsupportedOperationException();
    }
    if (maxAsynchWait > 0 && qhc.size() > maxAsynchWait) {
        throw new UnavailableFlowException(flowName);
    }
    BeanFlowInvoker invoker = (BeanFlowInvoker) factoryCallBack.createFlow(flowName);
    BeanFlowAsynchContext context = null;
    BeanFlowMonitor newMonitor = createMonitor();
    if (isReply) {
        ((BeanFlowMonitorImpl) monitor).addBeanFlowMonitor(newMonitor);
        final DefaultQueueService replyQueue = new DefaultQueueService();
        replyQueue.create();
        replyQueue.start();
        context = new BeanFlowAsynchContext(invoker, obj, newMonitor, replyQueue);
    } else {
        context = new BeanFlowAsynchContext(invoker, obj, newMonitor);
    }
    if (factoryCallBack.getThreadContext() != null) {
        context.putThreadContextAll(factoryCallBack.getThreadContext());
    }
    ((BeanFlowMonitorImpl) newMonitor).addAsynchContext(context);
    qhc.push(context);
    return context;
}
Also used : QueueHandlerContainer(jp.ossc.nimbus.service.queue.QueueHandlerContainer) BeanFlowAsynchContext(jp.ossc.nimbus.service.queue.BeanFlowAsynchContext) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService)

Example 3 with QueueHandlerContainer

use of jp.ossc.nimbus.service.queue.QueueHandlerContainer in project nimbus by nimbus-org.

the class DefaultGeneration method compete.

public void compete(int threadNum, long timeout) throws Exception {
    if (queueHandlerContainer == null && threadNum < 2) {
        for (int i = 0; i < seeds.length; i++) {
            if (seeds[i].getFitness() == null) {
                seeds[i].fit(this);
            }
        }
    } else {
        long start = System.currentTimeMillis();
        QueueHandlerContainer qhc = queueHandlerContainer;
        if (qhc == null) {
            QueueHandlerContainerService service = new QueueHandlerContainerService();
            service.create();
            service.setQueueHandler(new FitHandler());
            service.setQueueHandlerSize(threadNum);
            service.setQueueHandlerNowaitOnStop(true);
            service.setReleaseQueue(false);
            service.setIgnoreNullElement(true);
            service.setWaitTimeout(1000l);
            service.setQueueHandlerNowaitOnStop(true);
            service.start();
            qhc = service;
        } else {
            if (qhc.getQueueHandler() == null) {
                qhc.setQueueHandler(new FitHandler());
            }
        }
        DefaultQueueService responseQueue = new DefaultQueueService();
        responseQueue.create();
        responseQueue.start();
        for (int i = 0; i < seeds.length; i++) {
            qhc.push(new AsynchContext(new Object[] { this, seeds[i] }, responseQueue));
        }
        for (int i = 0; i < seeds.length; i++) {
            long currentTimeout = timeout > 0 ? timeout - (System.currentTimeMillis() - start) : timeout;
            if (timeout > 0 && currentTimeout <= 0) {
                throw new Exception("Compete timeout. timeout=" + timeout);
            }
            AsynchContext ctx = (AsynchContext) responseQueue.get(currentTimeout);
            if (ctx == null) {
                throw new Exception("Compete timeout. timeout=" + timeout);
            } else {
                try {
                    ctx.checkError();
                } catch (Exception e) {
                    throw e;
                } catch (Throwable th) {
                    throw (Error) th;
                }
            }
        }
        if (queueHandlerContainer == null) {
            ((QueueHandlerContainerService) qhc).stop();
            ((QueueHandlerContainerService) qhc).destroy();
        }
    }
    Arrays.sort(seeds, new SeedComparator(fitnessOrder));
}
Also used : QueueHandlerContainerService(jp.ossc.nimbus.service.queue.QueueHandlerContainerService) QueueHandlerContainer(jp.ossc.nimbus.service.queue.QueueHandlerContainer) AsynchContext(jp.ossc.nimbus.service.queue.AsynchContext) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService)

Example 4 with QueueHandlerContainer

use of jp.ossc.nimbus.service.queue.QueueHandlerContainer in project nimbus by nimbus-org.

the class SimpleGeneticAlgorithmService method createGeneration.

public Generation createGeneration(Random random, Seed seed, int seedNum, boolean isAsc) {
    DefaultGeneration generation = new DefaultGeneration();
    generation.setFitnessOrder(isAsc);
    generation.setConvergenceCondition(convergenceCondition);
    generation.setSeedSelection(isSeedSelection);
    generation.init(random, seed, seedNum);
    QueueHandlerContainer qhc = queueHandlerContainer;
    if (qhc == null && queueHandlerContainerServiceName != null) {
        qhc = (QueueHandlerContainer) ServiceManagerFactory.getServiceObject(queueHandlerContainerServiceName);
    }
    if (qhc != null) {
        generation.setQueueHandlerContainer(qhc);
    }
    return generation;
}
Also used : QueueHandlerContainer(jp.ossc.nimbus.service.queue.QueueHandlerContainer)

Example 5 with QueueHandlerContainer

use of jp.ossc.nimbus.service.queue.QueueHandlerContainer in project nimbus by nimbus-org.

the class SimpleGeneticAlgorithmService method compete.

public Seed compete(Random random, Seed seed, int seedNum, boolean isAsc) throws Exception {
    Generation generation = new DefaultGeneration();
    generation.setFitnessOrder(isAsc);
    generation.setConvergenceCondition(convergenceCondition);
    ((DefaultGeneration) generation).setSeedSelection(isSeedSelection);
    generation.init(random, seed, seedNum);
    QueueHandlerContainer qhc = queueHandlerContainer;
    if (qhc == null && queueHandlerContainerServiceName != null) {
        qhc = (QueueHandlerContainer) ServiceManagerFactory.getServiceObject(queueHandlerContainerServiceName);
    }
    if (qhc != null) {
        generation.setQueueHandlerContainer(qhc);
    }
    Generation nextGeneration = null;
    do {
        if (qhc == null && parallelThreadNum < 2) {
            generation.compete();
        } else {
            generation.compete(qhc == null ? parallelThreadNum : qhc.getQueueHandlerSize(), parallelResponseTimout);
        }
        nextGeneration = generation.next(random, seedMatchMaker);
        if (nextGeneration != null) {
            generation = nextGeneration;
        }
    } while (nextGeneration != null);
    return generation.getSurvivor();
}
Also used : QueueHandlerContainer(jp.ossc.nimbus.service.queue.QueueHandlerContainer)

Aggregations

QueueHandlerContainer (jp.ossc.nimbus.service.queue.QueueHandlerContainer)5 BeanFlowAsynchContext (jp.ossc.nimbus.service.queue.BeanFlowAsynchContext)2 DefaultQueueService (jp.ossc.nimbus.service.queue.DefaultQueueService)2 AsynchContext (jp.ossc.nimbus.service.queue.AsynchContext)1 QueueHandlerContainerService (jp.ossc.nimbus.service.queue.QueueHandlerContainerService)1