Skip to content

Commit

Permalink
job core
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Jan 9, 2024
1 parent ae588a6 commit e5524f4
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.linyuzai.job.core.concept;

import lombok.Getter;
import lombok.Setter;

import java.util.Map;

@Getter
@Setter
public abstract class AbstractJob implements Job {

private String id;

private String name;

private String description;

private Map<String, String> params;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package com.github.linyuzai.job.core.concept;

import java.util.Map;

public interface Job {

String getId();

String getName();

String getDescription();

Map<String, String> getParams();

Object run(Map<String, String> params) throws Throwable;

default Object run() throws Throwable {
return run(getParams());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
package com.github.linyuzai.job.core.concept;

public interface JobConcept {

void initialize();

void destroy();

void add(Object bean);

void update(Job job);

void remove(String jobId);

void start(String jobId);

void stop(String jobId);

Job get(String jobId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.linyuzai.job.core.concept;

import com.github.linyuzai.job.core.context.JobContext;
import com.github.linyuzai.job.core.context.JobContextImpl;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.lang.reflect.Method;
import java.util.Map;

@Getter
@RequiredArgsConstructor
public class MethodJob extends AbstractJob {

private final Object target;

private final Method method;

@Override
public Object run(Map<String, String> params) throws Throwable {
Class<?>[] types = method.getParameterTypes();
Object[] args = new Object[types.length];
for (int i = 0; i < types.length; i++) {
Class<?> type = types[i];
if (JobContext.class.isAssignableFrom(type)) {
JobContext context = new JobContextImpl(params);
args[i] = context;
}
if (Map.class.isAssignableFrom(type)) {
args[i] = params;
}
}
return method.invoke(target, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package com.github.linyuzai.job.core.context;

import java.util.Map;

public interface JobContext {

Map<String, String> getParams();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.linyuzai.job.core.context;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Map;

@Getter
@RequiredArgsConstructor
public class JobContextImpl implements JobContext {

private final Map<String, String> params;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.linyuzai.job.core.factory;

import java.util.Collection;

import com.github.linyuzai.job.core.concept.Job;

public interface JobFactory {

boolean support(Object o);

Collection<Job> create(Object o);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.linyuzai.job.core.factory;

import com.github.linyuzai.job.core.concept.Job;
import com.github.linyuzai.job.core.concept.MethodJob;
import com.github.linyuzai.job.core.context.JobContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MethodJobFactory implements JobFactory {

@Override
public boolean support(Object o) {
return o instanceof JobContext;
}

@Override
public Collection<Job> create(Object o) {
JobContext context = (JobContext) o;
List<Job> jobs = new ArrayList<>();
jobs.add(new MethodJob(null, null));
return jobs;
}
}

0 comments on commit e5524f4

Please sign in to comment.