parent
c7a24c0a4e
commit
c1363c7567
@ -0,0 +1,62 @@
|
||||
package com.xxl.job.core.callback;
|
||||
|
||||
import org.eclipse.jetty.server.Connector;
|
||||
import org.eclipse.jetty.server.Handler;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.HandlerCollection;
|
||||
import org.eclipse.jetty.server.nio.SelectChannelConnector;
|
||||
import org.eclipse.jetty.util.thread.ExecutorThreadPool;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.xxl.job.client.util.IpUtil;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 2016-5-22 11:15:42
|
||||
*/
|
||||
public class XxlJobCallbackServer {
|
||||
private static final Logger logger = LoggerFactory.getLogger(XxlJobCallbackServer.class);
|
||||
|
||||
private static String trigger_log_address;
|
||||
public static String getTrigger_log_address() {
|
||||
return trigger_log_address;
|
||||
}
|
||||
|
||||
public void start(int callBackPort) throws Exception {
|
||||
// init address
|
||||
|
||||
String ip = IpUtil.getIp();
|
||||
trigger_log_address = ip.concat(":").concat(String.valueOf(callBackPort));
|
||||
|
||||
final int port = Integer.valueOf(callBackPort);
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Server server = new Server();
|
||||
server.setThreadPool(new ExecutorThreadPool(200, 200, 30000)); // 非阻塞
|
||||
|
||||
// connector
|
||||
SelectChannelConnector connector = new SelectChannelConnector();
|
||||
connector.setPort(port);
|
||||
connector.setMaxIdleTime(30000);
|
||||
server.setConnectors(new Connector[] { connector });
|
||||
|
||||
// handler
|
||||
HandlerCollection handlerc =new HandlerCollection();
|
||||
handlerc.setHandlers(new Handler[]{new XxlJobCallbackServerHandler()});
|
||||
server.setHandler(handlerc);
|
||||
|
||||
try {
|
||||
server.start();
|
||||
logger.info(">>>>>>>>>>>> xxl-job XxlJobCallbackServer start success at port:{}.", port);
|
||||
server.join(); // block until server ready
|
||||
logger.info(">>>>>>>>>>>> xxl-job XxlJobCallbackServer join success at port:{}.", port);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.xxl.job.core.callback;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||
|
||||
import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
|
||||
import com.xxl.job.client.util.JacksonUtil;
|
||||
import com.xxl.job.core.model.XxlJobLog;
|
||||
import com.xxl.job.core.util.DynamicSchedulerUtil;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 2016-5-22 11:15:42
|
||||
*/
|
||||
public class XxlJobCallbackServerHandler extends AbstractHandler {
|
||||
|
||||
@Override
|
||||
public void handle(String s, Request baseRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
|
||||
|
||||
httpServletRequest.setCharacterEncoding("UTF-8");
|
||||
httpServletResponse.setCharacterEncoding("UTF-8");
|
||||
|
||||
// parse param
|
||||
String trigger_log_id = httpServletRequest.getParameter("trigger_log_id");
|
||||
String status = httpServletRequest.getParameter("status");
|
||||
String msg = httpServletRequest.getParameter("msg");
|
||||
|
||||
// process
|
||||
RemoteCallBack callBack = new RemoteCallBack();
|
||||
callBack.setStatus(RemoteCallBack.FAIL);
|
||||
if (StringUtils.isNumeric(trigger_log_id) && StringUtils.isNotBlank(status)) {
|
||||
XxlJobLog log = DynamicSchedulerUtil.xxlJobLogDao.load(Integer.valueOf(trigger_log_id));
|
||||
if (log!=null) {
|
||||
log.setHandleTime(new Date());
|
||||
log.setHandleStatus(status);
|
||||
log.setHandleMsg(msg);
|
||||
DynamicSchedulerUtil.xxlJobLogDao.updateHandleInfo(log);
|
||||
callBack.setStatus(RemoteCallBack.SUCCESS);
|
||||
}
|
||||
}
|
||||
String resp = JacksonUtil.writeValueAsString(callBack);
|
||||
|
||||
// response
|
||||
httpServletResponse.setContentType("text/html;charset=utf-8");
|
||||
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
|
||||
baseRequest.setHandled(true);
|
||||
httpServletResponse.getWriter().println(resp);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.xxl.job.client.util;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* get ip
|
||||
* @author xuxueli 2016-5-22 11:38:05
|
||||
*/
|
||||
public class IpUtil {
|
||||
private static final Logger logger = LoggerFactory.getLogger(IpUtil.class);
|
||||
|
||||
/**
|
||||
* 获取本机ip
|
||||
* @return
|
||||
*/
|
||||
public static String getIp() {
|
||||
try {
|
||||
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
InetAddress address = null;
|
||||
while (interfaces.hasMoreElements()) {
|
||||
NetworkInterface ni = interfaces.nextElement();
|
||||
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
address = addresses.nextElement();
|
||||
if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1) {
|
||||
return address.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.info("xxl job getHostAddress fail");
|
||||
return null;
|
||||
} catch (Throwable t) {
|
||||
logger.error("xxl job getHostAddress error, {}", t);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws UnknownHostException {
|
||||
System.out.println(InetAddress.getLocalHost().getCanonicalHostName());
|
||||
System.out.println(InetAddress.getLocalHost().getHostName());
|
||||
System.out.println(getIp());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue