JDeferred (#2553)
* moving jmh into libraries module * refactoring jmh * Update pom.xml * manual algorightm * with BM result * fix for space issue * Fixed indentation * change as per suggestion * vavr either * adding unit test and othe rutilities * adding concurrent module * concurrent package description * concurrent package description * Update EitherUnitTest.java * introducing lambda expression * jooby project * jooby project * reducing employee bean content * bootique module * bootique * bootique * undertow module * undertow module * refactoring * using lambda * as per baeldung formatter * easy code added for simplicity * simpliflying * adding JDeferred module * moved jdeferred to librarires module
This commit is contained in:
parent
d8398caaa2
commit
acf9bf7c4a
1207
libraries/pom.xml
1207
libraries/pom.xml
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.jdeffered;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
public class FilterDemo {
|
||||
|
||||
static String modifiedMsg;
|
||||
|
||||
public static String filter(String msg) {
|
||||
|
||||
Deferred<String, ?, ?> d = new DeferredObject<String, Object, Object>();
|
||||
Promise<String, ?, ?> p = d.promise();
|
||||
Promise<String, ?, ?> filtered = p.then((result) -> {
|
||||
modifiedMsg = "Hello " + result;
|
||||
});
|
||||
|
||||
filtered.done((result) -> {
|
||||
System.out.println("filtering done");
|
||||
});
|
||||
|
||||
d.resolve(msg);
|
||||
return modifiedMsg;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.jdeffered;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.DonePipe;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
public class PipeDemo {
|
||||
|
||||
public static enum Result {
|
||||
SUCCESS, FAILURE
|
||||
};
|
||||
|
||||
static Result status;
|
||||
|
||||
public static Result validate(int num) {
|
||||
Deferred<Integer, ?, ?> d = new DeferredObject<Integer, Object, Object>();
|
||||
Promise<Integer, ?, ?> p = d.promise();
|
||||
|
||||
p.then(new DonePipe<Integer, Integer, Exception, Void>() {
|
||||
public Deferred<Integer, Exception, Void> pipeDone(Integer result) {
|
||||
if (result < 90) {
|
||||
return new DeferredObject<Integer, Exception, Void>().resolve(result);
|
||||
} else {
|
||||
return new DeferredObject<Integer, Exception, Void>().reject(new Exception("Unacceptable value"));
|
||||
}
|
||||
}
|
||||
}).done((result) -> {
|
||||
status = Result.SUCCESS;
|
||||
}).fail((result) -> {
|
||||
status = Result.FAILURE;
|
||||
});
|
||||
|
||||
d.resolve(num);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.jdeffered;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
public class PromiseDemo {
|
||||
|
||||
public static void startJob(String jobName) {
|
||||
|
||||
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
|
||||
Promise<String, String, String> promise = deferred.promise();
|
||||
|
||||
promise.done((result) -> {
|
||||
System.out.println("Job done");
|
||||
}).fail((rejection) -> {
|
||||
System.out.println("Job fail");
|
||||
}).progress((progress) -> {
|
||||
System.out.println("Job is in progress");
|
||||
}).always((state, result, rejection) -> {
|
||||
System.out.println("Job execution started");
|
||||
});
|
||||
|
||||
deferred.resolve(jobName);
|
||||
// deferred.notify("");
|
||||
// deferred.reject("oops");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.jdeffered;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.DeferredManager;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DefaultDeferredManager;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
public class ThreadSafeDemo {
|
||||
|
||||
public static void task() {
|
||||
DeferredManager dm = new DefaultDeferredManager();
|
||||
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
|
||||
Promise<String, String, String> p1 = deferred.promise();
|
||||
Promise<String, String, String> p = dm.when(p1).done((result) -> {
|
||||
System.out.println("done");
|
||||
}).fail((result) -> {
|
||||
System.out.println("fail");
|
||||
});
|
||||
|
||||
synchronized (p) {
|
||||
while (p.isPending()) {
|
||||
try {
|
||||
p.wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
p.waitSafely();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
deferred.resolve("Hello Baeldung");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.jdeffered.manager;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.DeferredManager;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DefaultDeferredManager;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
public class DeferredManagerDemo {
|
||||
|
||||
public static void initiate() {
|
||||
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
|
||||
DeferredManager dm = new DefaultDeferredManager();
|
||||
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
|
||||
dm.when(p1, p2, p3).done((result) -> {
|
||||
System.out.println("done");
|
||||
}).fail((result) -> {
|
||||
System.out.println("fail");
|
||||
});
|
||||
deferred.resolve("Hello Baeldung");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.jdeffered.manager;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.DeferredManager;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DefaultDeferredManager;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
public class DeferredManagerWithExecutorDemo {
|
||||
|
||||
public static void initiate() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
|
||||
DeferredManager dm = new DefaultDeferredManager(executor);
|
||||
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
|
||||
dm.when(p1, p2, p3).done((result) -> {
|
||||
System.out.println("done");
|
||||
}).fail((result) -> {
|
||||
System.out.println("fail");
|
||||
});
|
||||
deferred.resolve("done");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.jdeffered.manager;
|
||||
|
||||
import org.jdeferred.DeferredManager;
|
||||
import org.jdeferred.impl.DefaultDeferredManager;
|
||||
|
||||
public class SimpleDeferredManagerDemo {
|
||||
|
||||
public static void initiate() {
|
||||
DeferredManager dm = new DefaultDeferredManager();
|
||||
dm.when(() -> {
|
||||
return 1;
|
||||
}).done((result) -> {
|
||||
System.out.println("done");
|
||||
}).fail((e) -> {
|
||||
e.printStackTrace();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.jdeffered;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jdeffered.PipeDemo.Result;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void givenJob_expectPromise() {
|
||||
PromiseDemo.startJob("Baeldung Job");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMsg_expectModifiedMsg() {
|
||||
String msg = FilterDemo.filter("Baeldung");
|
||||
assertEquals("Hello Baeldung", msg);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNum_validateNum_expectStatus() {
|
||||
Result result = PipeDemo.validate(80);
|
||||
assertEquals(result, Result.SUCCESS);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue