JAVA Json RPC
This commit is contained in:
snoop 2017-04-10 16:32:36 +09:00
parent c956e59c51
commit fca62deb01
3 changed files with 114 additions and 5 deletions

View File

@ -1,14 +1,35 @@
package com.loafle.rpc.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.googlecode.jsonrpc4j.JsonRpcMethod;
import com.googlecode.jsonrpc4j.JsonRpcService;
import com.loafle.rpc.model.Input;
import com.loafle.rpc.crawler.Crawler;
import com.loafle.rpc.model.Output;
@JsonRpcService("/rpc")
public interface Remote {
@JsonRpcMethod("Remote.Request")
Output request(Input t) throws JsonProcessingException;
@JsonRpcMethod("RPC.init")
Output init(String[] paths) throws JsonProcessingException;
@JsonRpcMethod("RPC.add")
Output add(String type, String id) throws JsonProcessingException ;
// @JsonRpcMethod("Remote.remove")
int addDelegate(String name , Crawler crawler);
@JsonRpcMethod("RPC.get")
Output get(String type, String id) throws JsonProcessingException;
@JsonRpcMethod("RPC.remove")
Output remove(String type, String id) throws JsonProcessingException;
}

View File

@ -2,23 +2,77 @@ package com.loafle.rpc.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import com.loafle.rpc.crawler.Crawler;
import com.loafle.rpc.model.Input;
import com.loafle.rpc.model.Output;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
@AutoJsonRpcServiceImpl
public class RemoteImpl implements Remote {
private Map<String, Crawler> crawlerMap = null;
@Override
public Output request(Input i) throws JsonProcessingException {
public Output init(String[] paths) throws JsonProcessingException {
for (String v : paths) {
System.out.println(v);
}
Output out = new Output();
out.setStartDate(new Date());
out.setEndDate(new Date());
out.setData("Not Delegate".getBytes());
out.setData("Method init".getBytes());
return out;
}
@Override
public Output add(String type, String id) throws JsonProcessingException {
return null;
}
@Override
public int addDelegate(String name, Crawler crawler) {
if(this.crawlerMap == null) {
this.crawlerMap = new HashMap<String, Crawler>();
}
if(this.crawlerMap.containsKey(name)) {
return 1;
}
this.crawlerMap.put(name, crawler);
return 0;
}
@Override
public Output get(String type, String id) throws JsonProcessingException {
Crawler crawler = this.crawlerMap.get(type);
if(crawler == null) {
System.out.println(type + "_crawler == null");
} else {
return crawler.get(id);
}
System.out.println("type : " + type);
System.out.println("id : " + id);
Output out = new Output();
out.setStartDate(new Date());
out.setEndDate(new Date());
out.setData("Not Delegate".getBytes());
return out;
}
@Override
public Output remove(String type, String id) throws JsonProcessingException {
return null;
}
}

View File

@ -0,0 +1,34 @@
package com.loafle.rpc.crawler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.loafle.rpc.model.Output;
/**
* Created by root on 17. 4. 10.
*/
public abstract class Crawler {
public Output add(String id) throws JsonProcessingException {
return null;
}
public abstract Output get(String id) throws JsonProcessingException ;
public Output getConfig(String id) throws JsonProcessingException {
return null;
}
public Output init(String config) throws JsonProcessingException {
return null;
}
public Output remove(String id) throws JsonProcessingException {
return null;
}
}