use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-ce by iitsoftware.
the class ShrinkJob method start.
public synchronized void start(Properties properties, JobTerminationListener jobTerminationListener) throws JobException {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.storeSwiftlet.getName(), toString() + "/start, properties=" + properties + " ...");
this.jobTerminationListener = jobTerminationListener;
this.properties = properties;
Semaphore sem = new Semaphore();
StartShrink po = new StartShrink(sem);
ctx.shrinkProcessor.enqueue(po);
sem.waitHere();
if (po.isSuccess())
jobTerminationListener.jobTerminated();
else
jobTerminationListener.jobTerminated(new JobException(po.getException(), new Exception(po.getException()), false));
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.storeSwiftlet.getName(), toString() + "/start, properties=" + properties + " ...");
}
use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-ce by iitsoftware.
the class LogManager method process.
protected void process(Object[] bulk, int n) {
if (closed && !checkPointPending)
return;
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process,length=" + n);
try {
if (logManagerListener != null)
logManagerListener.startProcessing();
boolean checkPointNow = false;
for (int i = 0; i < n; i++) {
LogOperation oper = (LogOperation) bulk[i];
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process, operation=" + oper);
switch(oper.getOperationType()) {
case LogOperation.OPER_LOG_REC:
LogRecord lr = (LogRecord) oper;
logFile.write(lr);
Semaphore s = lr.getSemaphore();
if (s != null)
semList.add(s);
CacheReleaseListener crl = lr.getCacheReleaseListener();
if (crl != null)
releaseList.add(crl);
AsyncCompletionCallback cb = lr.getCallback();
if (cb != null)
callbackList.add(cb);
// Releasing locks on shared message pages
List messagePageRefs = lr.getMessagePageRefs();
if (messagePageRefs != null) {
for (int j = 0; j < messagePageRefs.size(); j++) {
((MessagePageReference) messagePageRefs.get(j)).unMarkActive();
}
}
break;
case LogOperation.OPER_CLOSE_LOG:
closeSem = ((CloseLogOperation) oper).getSemaphore();
closed = true;
break;
case LogOperation.OPER_INITIATE_SYNC:
checkPointInitiated = true;
break;
case LogOperation.OPER_SYNC_LOG:
checkPointNow = true;
break;
}
}
// write the log to the log file
if (logFile.getFlushSize() > 0)
logFile.flush(forceSync);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process, processed log objects=" + n);
if (checkPointNow) {
if (checkPointVerbose)
System.out.println("+++ CHECKPOINT ... ");
long start = System.currentTimeMillis();
// Trigger CP Handler to flush the cache.
checkPointHandler.performCheckPoint();
// Start new log file
logFile.reset(forceSync);
// continue normal operation
checkPointHandler.checkPointDone();
checkPointPending = false;
if (checkPointVerbose)
System.out.println("+++ CHECKPOINT DONE in " + (System.currentTimeMillis() - start) + " milliseconds");
if (closed) {
if (logManagerListener != null)
logManagerListener.stopProcessing();
closeSem.notifySingleWaiter();
return;
}
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process, releaseList.size=" + releaseList.size() + ", semList.size=" + semList.size() + ", callbackList.size=" + callbackList.size());
// call back cache release listeners
if (releaseList.size() > 0) {
for (int i = 0; i < releaseList.size(); i++) {
CacheReleaseListener crl = (CacheReleaseListener) releaseList.get(i);
crl.releaseCache();
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process, " + releaseList.size() + " CacheReleaseListeners called");
releaseList.clear();
}
if (logManagerListener != null)
logManagerListener.stopProcessing();
// notify Semaphores
if (semList.size() > 0) {
for (int i = 0; i < semList.size(); i++) {
Semaphore ls = (Semaphore) semList.get(i);
ls.notifySingleWaiter();
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process, " + semList.size() + " semaphores notified");
semList.clear();
}
// notify callbacks
if (callbackList.size() > 0) {
for (int i = 0; i < callbackList.size(); i++) {
AsyncCompletionCallback cb = (AsyncCompletionCallback) callbackList.get(i);
cb.notifyCallbackStack(true);
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process, " + callbackList.size() + " AsyncCompletionCallbacks called");
callbackList.clear();
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process, logFile.length=" + logFile.getPosition());
// SyncLogOperation into the queue.
if (!checkPointPending && (closed || checkPointInitiated || logFile.getPosition() >= maxLogSize)) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process, checkPointHandler.lockForCheckPoint()");
checkPointHandler.lockForCheckPoint();
checkPointPending = true;
checkPointInitiated = false;
}
} catch (Exception e) {
// PANIC
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$store", toString() + "/process, exception occurred=" + e);
ctx.logSwiftlet.logError("sys$store", toString() + "/process, PANIC! EXITING! Exception occurred=" + e);
e.printStackTrace();
SwiftletManager.getInstance().disableShutdownHook();
System.exit(-1);
}
}
use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-client by iitsoftware.
the class JSSESocketFactory method createSocket.
public Socket createSocket(String host, int port, boolean useTcpNoDelay) throws UnknownHostException, IOException {
SSLSocket sslSocket = null;
if (HttpTunnelProperties.getInstance().isProxy() && HttpTunnelProperties.getInstance().isHostViaProxy(host)) {
Socket socket = new Socket(HttpTunnelProperties.getInstance().getProxyHost(), HttpTunnelProperties.getInstance().getProxyPort());
HttpTunnelProperties.getInstance().setupHttpProxy(host, port, socket.getInputStream(), socket.getOutputStream());
sslSocket = (SSLSocket) getSocketFactory().createSocket(socket, host, port, true);
} else
sslSocket = (SSLSocket) getSocketFactory().createSocket(host, port);
if (SO_TIMEOUT > 0) {
try {
sslSocket.setSoTimeout(SO_TIMEOUT);
} catch (SocketException e) {
System.err.println("JSSESocketFactory: Unable to perform 'socket.setSoTimeout(" + SO_TIMEOUT + ")', exception: " + e);
}
}
sslSocket.setTcpNoDelay(useTcpNoDelay);
Semaphore sem = new Semaphore();
sslSocket.addHandshakeCompletedListener(new CompletionListener(sem));
sslSocket.startHandshake();
sem.waitHere();
return sslSocket;
}
use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-ce by iitsoftware.
the class Forwarder method forward.
public void forward() {
sem = new Semaphore();
sem.waitHere();
if (exception != null)
failFast("failed: " + exception);
try {
verifier.verify();
} catch (Exception e) {
failFast("failed: " + e);
}
}
use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-ce by iitsoftware.
the class Consumer method consume.
public void consume() {
sem = new Semaphore();
sem.waitHere();
if (exception != null)
failFast("failed: " + exception);
try {
verifier.verify();
} catch (Exception e) {
failFast("failed: " + e);
}
}
Aggregations