Search in sources :

Example 1 with WaitSynchronizeMonitor

use of jp.ossc.nimbus.util.WaitSynchronizeMonitor in project nimbus by nimbus-org.

the class HttpResponseCacheInterceptorService method getHttpResponseCache.

private HttpResponseCache getHttpResponseCache(String path) throws InterruptedException {
    HttpResponseCache responseCache = (HttpResponseCache) cacheMap.get(path);
    if (responseCache != null) {
        return responseCache;
    }
    SynchronizeMonitor waitMonitor = (SynchronizeMonitor) lockMap.get(path);
    if (waitMonitor == null) {
        responseCache = (HttpResponseCache) cacheMap.get(path);
        if (responseCache == null) {
            waitMonitor = (SynchronizeMonitor) lockMap.putIfAbsent(path, new WaitSynchronizeMonitor());
        } else {
            return responseCache;
        }
    }
    if (waitMonitor == null) {
        return null;
    }
    waitMonitor.initMonitor();
    if (waitTimeout > 0) {
        if (waitMonitor.waitMonitor(waitTimeout)) {
            return getHttpResponseCache(path);
        } else {
            return null;
        }
    } else {
        waitMonitor.waitMonitor();
        return getHttpResponseCache(path);
    }
}
Also used : SynchronizeMonitor(jp.ossc.nimbus.util.SynchronizeMonitor) WaitSynchronizeMonitor(jp.ossc.nimbus.util.WaitSynchronizeMonitor) WaitSynchronizeMonitor(jp.ossc.nimbus.util.WaitSynchronizeMonitor)

Example 2 with WaitSynchronizeMonitor

use of jp.ossc.nimbus.util.WaitSynchronizeMonitor in project nimbus by nimbus-org.

the class ClientConnectionImpl method send.

private void send(ClientMessage message) throws IOException, MessageSendException {
    SynchronizeMonitor responseMonitor = null;
    Short reqId = null;
    boolean isBye = message.getMessageType() == ClientMessage.MESSAGE_BYE;
    try {
        if (!isBye && isAcknowledge) {
            if (requestMonitorMap == null) {
                requestMonitorMap = new HashMap();
            }
            synchronized (requestMonitorMap) {
                message.setRequestId(requestId++);
                responseMonitor = new WaitSynchronizeMonitor();
                responseMonitor.initMonitor();
                reqId = new Short(message.getRequestId());
                requestMonitorMap.put(reqId, responseMonitor);
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (externalizer == null) {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(message);
            oos.flush();
        } else {
            externalizer.writeExternal(message, baos);
        }
        byte[] bytes = baos.toByteArray();
        synchronized (this) {
            if (socket == null) {
                throw new MessageSendException("No connected.");
            }
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            dos.writeInt(bytes.length);
            dos.write(bytes);
            dos.flush();
        }
        if (!isBye && isAcknowledge) {
            try {
                if (!responseMonitor.waitMonitor(responseTimeout)) {
                    throw new MessageSendException("Acknowledge is timed out.");
                }
            } catch (InterruptedException e) {
                throw new MessageSendException("Acknowledge is interrupted.", e);
            }
        }
    } finally {
        if (!isBye && isAcknowledge) {
            synchronized (requestMonitorMap) {
                requestMonitorMap.remove(reqId);
            }
        }
    }
}
Also used : WaitSynchronizeMonitor(jp.ossc.nimbus.util.WaitSynchronizeMonitor) SynchronizeMonitor(jp.ossc.nimbus.util.SynchronizeMonitor) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) WaitSynchronizeMonitor(jp.ossc.nimbus.util.WaitSynchronizeMonitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 3 with WaitSynchronizeMonitor

use of jp.ossc.nimbus.util.WaitSynchronizeMonitor in project nimbus by nimbus-org.

the class ServiceManagerFactory method main.

/**
 * コンパイルコマンドを実行する。<p>
 * <pre>
 * コマンド使用方法:
 *  java jp.ossc.nimbus.core.ServiceManagerFactory [options] [paths]
 *
 * [options]
 *
 *  [-validate]
 *   サービス定義をDTDで検証する。
 *
 *  [-server]
 *   メインスレッドを待機させて、サーバとして動かす。
 *
 *  [-help]
 *   ヘルプを表示します。
 *
 * [paths]
 *  ロードするサービス定義ファイルのパス
 *
 * 使用例 :
 *    java -classpath classes;lib/nimbus.jar jp.ossc.nimbus.core.ServiceManagerFactory service-definition.xml
 * </pre>
 *
 * @param args コマンド引数
 * @exception Exception コンパイル中に問題が発生した場合
 */
public static void main(String[] args) throws Exception {
    if (args.length != 0 && args[0].equals("-help")) {
        usage();
        return;
    }
    final List servicePaths = new ArrayList();
    boolean validate = false;
    boolean server = false;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-server")) {
            server = true;
        } else if (args[i].equals("-validate")) {
            validate = true;
        } else {
            servicePaths.add(args[i]);
        }
    }
    if (servicePaths.size() == 0) {
        usage();
        return;
    }
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        public void run() {
            for (int i = servicePaths.size(); --i >= 0; ) {
                ServiceManagerFactory.unloadManager((String) servicePaths.get(i));
            }
        }
    }));
    for (int i = 0, max = servicePaths.size(); i < max; i++) {
        if (!ServiceManagerFactory.loadManager((String) servicePaths.get(i), false, validate)) {
            Thread.sleep(1000);
            System.exit(-1);
        }
    }
    if (!ServiceManagerFactory.checkLoadManagerCompleted()) {
        Thread.sleep(1000);
        System.exit(-1);
    }
    if (server) {
        WaitSynchronizeMonitor lock = new WaitSynchronizeMonitor();
        synchronized (lock) {
            lock.initMonitor();
            try {
                lock.waitMonitor();
            } catch (InterruptedException ignore) {
            }
        }
    }
}
Also used : WaitSynchronizeMonitor(jp.ossc.nimbus.util.WaitSynchronizeMonitor)

Example 4 with WaitSynchronizeMonitor

use of jp.ossc.nimbus.util.WaitSynchronizeMonitor in project nimbus by nimbus-org.

the class MemorySemaphore method readObject.

/**
 * デシリアライズを行う。<p>
 *
 * @param in デシリアライズの元情報となるストリーム
 * @exception IOException 読み込みに失敗した場合
 * @exception ClassNotFoundException デシリアライズしようとしたオブジェクトのクラスが見つからない場合
 */
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
    in.defaultReadObject();
    getMonitor = new WaitSynchronizeMonitor();
    usedThreads = new ConcurrentHashMap();
    threadTasks = new ConcurrentHashMap();
    forceFreeTimer = new Timer(true);
}
Also used : WaitSynchronizeMonitor(jp.ossc.nimbus.util.WaitSynchronizeMonitor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

WaitSynchronizeMonitor (jp.ossc.nimbus.util.WaitSynchronizeMonitor)4 SynchronizeMonitor (jp.ossc.nimbus.util.SynchronizeMonitor)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MessageSendException (jp.ossc.nimbus.service.publish.MessageSendException)1