You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Step 1. Add the JitPack repository to your build file
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Step 2. Add the dependency
<dependency>
<groupId>com.github.itsoulltd</groupId>
<artifactId>JFoundationKit</artifactId>
<version>latest</version>
</dependency>
Programming philosophy behind:
1. DRY (Don't repeat yourself and Put shared logic in one place).
2. The Single Responsibility Principle (SRP) (Keep each component focused on one responsibility).
3. Composition over inheritance (*More flexible; *Easier to modify behavior at runtime; *Reduces tight coupling between classes).
public class ExampleTask extends AbstractTask<Message, Response> {
//Either override default constructor:
public ExampleTask() {super();}
//OR
//Provide an custom constructor:
public ExampleTask(String data) {
super(new Property("data", data));
}
@Override
public Response execute(Message message) throws RuntimeException {
String savedData = getPropertyValue("data").toString();
//....
//....
return new Response().setMessage(savedData).setStatus(200);
}
@Override
public Response abort(Message message) throws RuntimeException {
String reason = message != null ? message.getPayload() : "UnknownError!";
return new Response().setMessage(reason).setStatus(500);
}
}
Create and Running Task Using TaskStack:
###Defining a TaskStack:
private TaskStack stack = new TransactionStack();
stack.push(new SimpleTask("Wow bro! I am Adams"));
stack.push(new SimpleTask("Hello bro! I am Hayes"));
stack.push(new SimpleTask("Hi there! I am Cris", (message) -> {
Event event = message.getEvent(Event.class);
event.setMessage("Converted Message");
event.setStatus(201);
message.setEvent(event);
return message;
}));
stack.push(new SimpleTask("Let's bro! I am James"));
stack.commit(false, (result, state) -> {
System.out.println("State: " + state.name());
System.out.println(result.toString());
latch.countDown();
});
### Output:
Doing jobs...Let's bro! I am James
Doing jobs...Hi there! I am Cris
Doing jobs...Hello bro! I am Hayes
{"message":"Converted Message","status":201}
Doing jobs...Wow bro! I am Adams
State: Finished
{"payload":"{\"message\":\"Converted Message\",\"status\":201}","status":200}
### Doing Abort
stack.push(new SimpleTask("Wow bro! I am Adams"));
stack.push(new AbortTask("Hello bro! I am Hayes"));
stack.push(new SimpleTask("Hi there! I am Cris"));
stack.push(new SimpleTask("Let's bro! I am James"));
stack.commit(false, (result, state) -> {
System.out.println("State: " + state.name());
System.out.println(result.toString());
latch.countDown();
});
### Output:
Doing jobs...Let's bro! I am James
Doing jobs...Hi there! I am Cris
Doing revert ...:Hello bro! I am Hayes
Doing revert ...:Hi there! I am Cris
Doing revert ...:Let's bro! I am James
State: Failed
{"payload":"{\"status\":500,\"error\":\"I AM Aborting! Critical Error @ (Hello bro! I am Hayes)\"}","status":502}
/**
* SimpleDataSource is a concreate impl of iDataSource interface.
*/
//Create an instance of iDataSource from a concreate impl of SimpleDataSource.java
SimpleDataSource<String, Object> dataSource = new SimpleDataSource<>();
//API: Create and Insert:
Person person = new Person().setName("John")
.setEmail("john@gmail.com").setAge(36)
.setGender("male");
dataSource.put("id-001", person);
person = new Person().setName("Adam")
.setEmail("adam@gmail.com").setAge(31)
.setGender("male");
dataSource.put("id-002", person);
//API: Read by Key
Person found = dataSource.read("id-001");
//API: Paginated read-sync and Convert:
int maxItem = dataSource.size();
Object[] items = dataSource.readSync(0, maxItem);
List<Person> converted = Stream.of(items).map(itm -> (Person) itm).collect(Collectors.toList());
converted.forEach(person -> System.out.println(person.toString()));
//API: Remove
Person removed = dataSource.remove("id-002");
//API: Replace
Person replaced = dataSource.replace("id-002", new Person()...);
Page Vs Offset:
//Page Vs Offset: When limit/size is given
public int getOffset(int page, int limit) {
if (limit <= 0) limit = 10;
if (page <= 0) page = 1;
int offset = (page - 1) * limit;
return offset;
}
//E.g. Usually in rest-api get-method, page variable being passed with starting value from 1;
//Where as in database sql-context, we write select query with offset with startting value from 0;
//So, usually we have to translate page into offset or vice-versa.
int page = 2;
int limit = 10;
int offset = getOffset(page, limit);
//Test Results:
When (limit:10 & page:2) Offset: 10
When (limit:10 & page:-1) Offset: 0
When (limit:10 & page:7) Offset: 60
When (limit:10 & page:101) Offset: 1000
When (limit:15 & page:2) Offset: 15
When (limit:15 & page:-1) Offset: 0
When (limit:20 & page:7) Offset: 120
When (limit:-1 & page:-1) Offset: 0
To know more about Task & TaskStack & TaskQueue, visit test classes. Thank you!