Skip to content

Commit

Permalink
Issue #6556 Use FuturePromise<SessionData>
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Jul 29, 2021
1 parent 9679b0c commit d438a1b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 66 deletions.
Expand Up @@ -21,11 +21,11 @@
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.transcoders.SerializingTranscoder;
import org.eclipse.jetty.server.session.RunnableResult;
import org.eclipse.jetty.server.session.SessionContext;
import org.eclipse.jetty.server.session.SessionData;
import org.eclipse.jetty.server.session.SessionDataMap;
import org.eclipse.jetty.util.ClassLoadingObjectInputStream;
import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
Expand Down Expand Up @@ -164,17 +164,17 @@ public SessionData load(String id) throws Exception
if (!isStarted())
throw new IllegalStateException("Not started");

final RunnableResult<SessionData> result = new RunnableResult<>();
final FuturePromise<SessionData> result = new FuturePromise<>();

Runnable r = () ->
{
try
{
result.setResult(_client.get(id));
result.succeeded(_client.get(id));
}
catch (Exception e)
{
result.setException(e);
result.failed(e);
}
};

Expand All @@ -188,20 +188,21 @@ public void store(String id, SessionData data) throws Exception
if (!isStarted())
throw new IllegalStateException("Not started");

final RunnableResult<Object> result = new RunnableResult<>();
final FuturePromise<Boolean> result = new FuturePromise<>();
Runnable r = () ->
{
try
{
_client.set(id, _expirySec, data);
result.succeeded(Boolean.TRUE);
}
catch (Exception e)
{
result.setException(e);
result.failed(e);
}
};
_context.run(r);
result.throwIfException();
result.getOrThrow();
}

@Override
Expand Down
Expand Up @@ -15,8 +15,10 @@

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
Expand Down Expand Up @@ -136,21 +138,22 @@ public SessionData load(String id) throws Exception
if (!isStarted())
throw new IllegalStateException("Not started");

final RunnableResult<SessionData> result = new RunnableResult<>();

final FuturePromise<SessionData> result = new FuturePromise<>();
Runnable r = () ->
{
try
{
result.setResult(doLoad(id));
result.succeeded(doLoad(id));
}
catch (Exception e)
{
result.setException(e);
result.failed(e);
}
};

_context.run(r);

return result.getOrThrow();
}

Expand Down Expand Up @@ -179,40 +182,41 @@ public void store(String id, SessionData data) throws Exception
//set the last saved time to now
data.setLastSaved(System.currentTimeMillis());

final RunnableResult<Object> result = new RunnableResult<>();
final FuturePromise<Boolean> result = new FuturePromise<>();
Runnable r = () ->
{
try
{
//call the specific store method, passing in previous save time
doStore(id, data, lastSave);
data.clean(); //unset all dirty flags
result.succeeded(Boolean.TRUE);
}
catch (Exception e)
{
//reset last save time if save failed
data.setLastSaved(lastSave);
result.setException(e);
result.failed(e);
}
};
_context.run(r);
result.throwIfException();
result.getOrThrow();
}
}

@Override
public boolean exists(String id) throws Exception
{
RunnableResult<Boolean> result = new RunnableResult<>();
FuturePromise<Boolean> result = new FuturePromise<>();
Runnable r = () ->
{
try
{
result.setResult(doExists(id));
result.succeeded(doExists(id));
}
catch (Exception e)
{
result.setException(e);
result.failed(e);
}
};

Expand Down

This file was deleted.

21 changes: 21 additions & 0 deletions jetty-util/src/main/java/org/eclipse/jetty/util/FuturePromise.java
Expand Up @@ -118,6 +118,27 @@ public C get() throws InterruptedException, ExecutionException
throw (CancellationException)new CancellationException().initCause(_cause);
throw new ExecutionException(_cause);
}

/**
* Return the result if completed successfully
* or rethrow the Exception
*
* @return the computed result
* @throws Exception
*/
public C getOrThrow() throws Exception
{
_latch.await();

if (_cause == COMPLETED)
return _result;
if (_cause instanceof Exception)
throw (Exception)_cause;
if (_cause instanceof Error)
throw (Error)_cause;

throw new ExecutionException(_cause);
}

@Override
public C get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
Expand Down

0 comments on commit d438a1b

Please sign in to comment.