Refactor JDeffered (#2559)
This commit is contained in:
parent
7b836d9211
commit
cafdf51bd7
|
@ -4,25 +4,21 @@ import org.jdeferred.Deferred;
|
||||||
import org.jdeferred.Promise;
|
import org.jdeferred.Promise;
|
||||||
import org.jdeferred.impl.DeferredObject;
|
import org.jdeferred.impl.DeferredObject;
|
||||||
|
|
||||||
public class FilterDemo {
|
class FilterDemo {
|
||||||
|
|
||||||
static String modifiedMsg;
|
private static String modifiedMsg;
|
||||||
|
|
||||||
public static String filter(String msg) {
|
static String filter(String msg) {
|
||||||
|
|
||||||
Deferred<String, ?, ?> d = new DeferredObject<String, Object, Object>();
|
Deferred<String, ?, ?> d = new DeferredObject<>();
|
||||||
Promise<String, ?, ?> p = d.promise();
|
Promise<String, ?, ?> p = d.promise();
|
||||||
Promise<String, ?, ?> filtered = p.then((result) -> {
|
Promise<String, ?, ?> filtered = p.then((result) -> {
|
||||||
modifiedMsg = "Hello " + result;
|
modifiedMsg = "Hello " + result;
|
||||||
});
|
});
|
||||||
|
|
||||||
filtered.done((result) -> {
|
filtered.done(r -> System.out.println("filtering done"));
|
||||||
System.out.println("filtering done");
|
|
||||||
});
|
|
||||||
|
|
||||||
d.resolve(msg);
|
d.resolve(msg);
|
||||||
return modifiedMsg;
|
return modifiedMsg;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,35 +5,31 @@ import org.jdeferred.DonePipe;
|
||||||
import org.jdeferred.Promise;
|
import org.jdeferred.Promise;
|
||||||
import org.jdeferred.impl.DeferredObject;
|
import org.jdeferred.impl.DeferredObject;
|
||||||
|
|
||||||
public class PipeDemo {
|
class PipeDemo {
|
||||||
|
|
||||||
public static enum Result {
|
public enum Result {
|
||||||
SUCCESS, FAILURE
|
SUCCESS, FAILURE
|
||||||
};
|
};
|
||||||
|
|
||||||
static Result status;
|
private static Result status;
|
||||||
|
|
||||||
public static Result validate(int num) {
|
static Result validate(int num) {
|
||||||
Deferred<Integer, ?, ?> d = new DeferredObject<Integer, Object, Object>();
|
Deferred<Integer, ?, ?> d = new DeferredObject<>();
|
||||||
Promise<Integer, ?, ?> p = d.promise();
|
Promise<Integer, ?, ?> p = d.promise();
|
||||||
|
|
||||||
p.then(new DonePipe<Integer, Integer, Exception, Void>() {
|
p.then((DonePipe<Integer, Integer, Exception, Void>) result -> {
|
||||||
public Deferred<Integer, Exception, Void> pipeDone(Integer result) {
|
if (result < 90) {
|
||||||
if (result < 90) {
|
return new DeferredObject<Integer, Exception, Void>()
|
||||||
return new DeferredObject<Integer, Exception, Void>().resolve(result);
|
.resolve(result);
|
||||||
} else {
|
} else {
|
||||||
return new DeferredObject<Integer, Exception, Void>().reject(new Exception("Unacceptable value"));
|
return new DeferredObject<Integer, Exception, Void>()
|
||||||
}
|
.reject(new Exception("Unacceptable value"));
|
||||||
}
|
}
|
||||||
}).done((result) -> {
|
}).done(r -> status = Result.SUCCESS)
|
||||||
status = Result.SUCCESS;
|
.fail(r -> status = Result.FAILURE);
|
||||||
}).fail((result) -> {
|
|
||||||
status = Result.FAILURE;
|
|
||||||
});
|
|
||||||
|
|
||||||
d.resolve(num);
|
d.resolve(num);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,22 +4,17 @@ import org.jdeferred.Deferred;
|
||||||
import org.jdeferred.Promise;
|
import org.jdeferred.Promise;
|
||||||
import org.jdeferred.impl.DeferredObject;
|
import org.jdeferred.impl.DeferredObject;
|
||||||
|
|
||||||
public class PromiseDemo {
|
class PromiseDemo {
|
||||||
|
|
||||||
public static void startJob(String jobName) {
|
static void startJob(String jobName) {
|
||||||
|
|
||||||
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
|
Deferred<String, String, String> deferred = new DeferredObject<>();
|
||||||
Promise<String, String, String> promise = deferred.promise();
|
Promise<String, String, String> promise = deferred.promise();
|
||||||
|
|
||||||
promise.done((result) -> {
|
promise.done(result -> System.out.println("Job done"))
|
||||||
System.out.println("Job done");
|
.fail(rejection -> System.out.println("Job fail"))
|
||||||
}).fail((rejection) -> {
|
.progress(progress -> System.out.println("Job is in progress"))
|
||||||
System.out.println("Job fail");
|
.always((state, result, rejection) -> System.out.println("Job execution started"));
|
||||||
}).progress((progress) -> {
|
|
||||||
System.out.println("Job is in progress");
|
|
||||||
}).always((state, result, rejection) -> {
|
|
||||||
System.out.println("Job execution started");
|
|
||||||
});
|
|
||||||
|
|
||||||
deferred.resolve(jobName);
|
deferred.resolve(jobName);
|
||||||
// deferred.notify("");
|
// deferred.notify("");
|
||||||
|
|
|
@ -10,13 +10,11 @@ public class ThreadSafeDemo {
|
||||||
|
|
||||||
public static void task() {
|
public static void task() {
|
||||||
DeferredManager dm = new DefaultDeferredManager();
|
DeferredManager dm = new DefaultDeferredManager();
|
||||||
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
|
Deferred<String, String, String> deferred = new DeferredObject<>();
|
||||||
Promise<String, String, String> p1 = deferred.promise();
|
Promise<String, String, String> p1 = deferred.promise();
|
||||||
Promise<String, String, String> p = dm.when(p1).done((result) -> {
|
Promise<String, String, String> p = dm.when(p1)
|
||||||
System.out.println("done");
|
.done(r -> System.out.println("done"))
|
||||||
}).fail((result) -> {
|
.fail(r -> System.out.println("fail"));
|
||||||
System.out.println("fail");
|
|
||||||
});
|
|
||||||
|
|
||||||
synchronized (p) {
|
synchronized (p) {
|
||||||
while (p.isPending()) {
|
while (p.isPending()) {
|
||||||
|
|
|
@ -6,10 +6,10 @@ import org.jdeferred.Promise;
|
||||||
import org.jdeferred.impl.DefaultDeferredManager;
|
import org.jdeferred.impl.DefaultDeferredManager;
|
||||||
import org.jdeferred.impl.DeferredObject;
|
import org.jdeferred.impl.DeferredObject;
|
||||||
|
|
||||||
public class DeferredManagerDemo {
|
class DeferredManagerDemo {
|
||||||
|
|
||||||
public static void initiate() {
|
public static void initiate() {
|
||||||
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
|
Deferred<String, String, String> deferred = new DeferredObject<>();
|
||||||
DeferredManager dm = new DefaultDeferredManager();
|
DeferredManager dm = new DefaultDeferredManager();
|
||||||
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
|
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
|
||||||
dm.when(p1, p2, p3).done((result) -> {
|
dm.when(p1, p2, p3).done((result) -> {
|
||||||
|
@ -19,5 +19,4 @@ public class DeferredManagerDemo {
|
||||||
});
|
});
|
||||||
deferred.resolve("Hello Baeldung");
|
deferred.resolve("Hello Baeldung");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,19 +9,16 @@ import org.jdeferred.Promise;
|
||||||
import org.jdeferred.impl.DefaultDeferredManager;
|
import org.jdeferred.impl.DefaultDeferredManager;
|
||||||
import org.jdeferred.impl.DeferredObject;
|
import org.jdeferred.impl.DeferredObject;
|
||||||
|
|
||||||
public class DeferredManagerWithExecutorDemo {
|
class DeferredManagerWithExecutorDemo {
|
||||||
|
|
||||||
public static void initiate() {
|
public static void initiate() {
|
||||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||||
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
|
Deferred<String, String, String> deferred = new DeferredObject<>();
|
||||||
DeferredManager dm = new DefaultDeferredManager(executor);
|
DeferredManager dm = new DefaultDeferredManager(executor);
|
||||||
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
|
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
|
||||||
dm.when(p1, p2, p3).done((result) -> {
|
dm.when(p1, p2, p3)
|
||||||
System.out.println("done");
|
.done(r -> System.out.println("done"))
|
||||||
}).fail((result) -> {
|
.fail(r -> System.out.println("fail"));
|
||||||
System.out.println("fail");
|
|
||||||
});
|
|
||||||
deferred.resolve("done");
|
deferred.resolve("done");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,17 +3,12 @@ package com.baeldung.jdeffered.manager;
|
||||||
import org.jdeferred.DeferredManager;
|
import org.jdeferred.DeferredManager;
|
||||||
import org.jdeferred.impl.DefaultDeferredManager;
|
import org.jdeferred.impl.DefaultDeferredManager;
|
||||||
|
|
||||||
public class SimpleDeferredManagerDemo {
|
class SimpleDeferredManagerDemo {
|
||||||
|
|
||||||
public static void initiate() {
|
public static void initiate() {
|
||||||
DeferredManager dm = new DefaultDeferredManager();
|
DeferredManager dm = new DefaultDeferredManager();
|
||||||
dm.when(() -> {
|
dm.when(() -> 1)
|
||||||
return 1;
|
.done(r -> System.out.println("done"))
|
||||||
}).done((result) -> {
|
.fail(Throwable::printStackTrace);
|
||||||
System.out.println("done");
|
|
||||||
}).fail((e) -> {
|
|
||||||
e.printStackTrace();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
package com.baeldung.jdeffered;
|
package com.baeldung.jdeffered;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.baeldung.jdeffered.PipeDemo.Result;
|
import com.baeldung.jdeffered.PipeDemo.Result;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
@ -24,5 +23,4 @@ public class AppTest {
|
||||||
Result result = PipeDemo.validate(80);
|
Result result = PipeDemo.validate(80);
|
||||||
assertEquals(result, Result.SUCCESS);
|
assertEquals(result, Result.SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue