NutzCN Logo
问答 Mvcs.getReq() 报NULL
发布于 2090天前 作者 wx_jh90gbtqnaro5f7sm3rt 1536 次浏览 复制 上一个帖子 下一个帖子
标签:

刚查资料,应该是所在的类不在作用域里,但是不知道怎么配置,大佬请指点指点,感谢!

12 回复

这跟配置无关,作用域不是配出来,有请求才有req对象

@wendal 这样 public Object doLogin(@Attr("loginToken") AuthenticationToken token, HttpServletRequest req, HttpSession session) {} 的方法中,HttpServletRequest req 算是请求吗?

要从调用层次(堆栈)来看

@wendal 从客户端向服务端发送请求产生req对象,我是从定时任务调用另一个方法怎么产生请求呢?(基础不好。。。。)

产生请求?Http.get发起咯,但一般不会这样搞

既然是同一个系统内,调用service方法就行啦

把共用逻辑放到service层,做个类

@wendal 我是这样写的 还是为null

@IocBean(args = {"refer:dao"})
public class ZuzhijigouService extends Service<sw_zuzhijigou> {

	private static final Log log = Logs.get();
	
    public ZuzhijigouService(Dao dao) {
        super(dao);
    }
    
    XmlanlayService xmlanlayService;
 	
    @Inject
    SysLogService sysLogService;
 	//操作数据库将数据存入数据库
 	public int save(File file) throws DocumentException, ParseException{
 		
 		int r = 1;
 		List<Map<String,String>> list = xmlanlayService.xmlChangeString(file);
 		if(list.size()!=0) {
 			//逻辑处理
 	        			
 	        			
 	    		sysLogService.async(Sys_log.c("info", "执行存入日志", "执行成功", null));
 	    	}else {
 	    		log.info("无返回数据");
 	    	}
 		return r;
    		
    }

sysLogService(不是我写的类)

package cn.wizzer.common.services.log;

import cn.wizzer.modules.back.sys.models.Sys_log;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * Created by wizzer on 2016/6/22.
 */
@IocBean(create="init", depose="close")
public class SysLogService implements Runnable {

    private static final Log log = Logs.get();

    ExecutorService es;

    LinkedBlockingQueue<Sys_log> queue;

    @Inject
    protected Dao dao;

    public void async(Sys_log syslog) {
        LinkedBlockingQueue<Sys_log> queue = this.queue;
        if (queue != null)
            try {
                boolean re = queue.offer(syslog, 50, TimeUnit.MILLISECONDS);
                if (!re) {
                    log.info("syslog queue is full, drop it ...");
                }
            } catch (InterruptedException e) {
            }
    }

    public void sync(Sys_log syslog) {
        try {
            dao.fastInsert(syslog);
        } catch (Throwable e) {
            log.info("insert syslog sync fail", e);
        }
    }

    public void run() {
        while (true) {
            LinkedBlockingQueue<Sys_log> queue = this.queue;
            if (queue == null)
                break;
            try {
                Sys_log sysLog = queue.poll(1, TimeUnit.SECONDS);
                if (sysLog != null) {
                    sync(sysLog);
                }
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    public void init() {
        queue = new LinkedBlockingQueue<Sys_log>();
        int c = Runtime.getRuntime().availableProcessors();
        es = Executors.newFixedThreadPool(c);
        for (int i = 0; i < c; i++) {
            es.submit(this);
        }
    }

    public void close() throws InterruptedException {
        queue = null; // 触发关闭
        if (es != null && !es.isShutdown()) {
            es.shutdown();
            es.awaitTermination(5, TimeUnit.SECONDS);
        }
    }
}

Sys_log (也不是我写的类)

 public static Sys_log c(String type, String tag, String msg, String source) {
        Sys_log sysLog = new Sys_log();
        if (type == null || tag == null || msg == null) {
            throw new RuntimeException("type/tag/msg can't null");
        }
        if (source == null) {
            StackTraceElement[] tmp = Thread.currentThread().getStackTrace();
            if (tmp.length > 2) {
                source = tmp[2].getClassName() + "#" + tmp[2].getMethodName();
            } else {
                source = "main";
            }

        }
        sysLog.type = type;
        sysLog.tag = tag;
        sysLog.src = source;
        sysLog.msg = msg;
        sysLog.ip = StringUtil.getRemoteAddr();
        Object u = SecurityUtils.getSubject().getPrincipal();
        String uid = "";
        String nickname = "";
        if (u != null) {
            if (u instanceof Sys_user) {
                nickname = ((Sys_user) u).getNickname();
                uid = ((Sys_user) u).getId();
            } else if (u instanceof String) {
                nickname = ((String) u);
            }
        }
        sysLog.setOpBy(uid);
        sysLog.setOpAt((int) (System.currentTimeMillis() / 1000));
        sysLog.nickname = nickname;
        return sysLog;
    }

你是关心syslog实现里面取用户id的逻辑??? try-catch隔离掉就行了

@wendal 是取ip 调用的类(下面的方法)调取 Mvcs.getReq(); 为空 想弄明白什么原理。。。

public static String getRemoteAddr() {
        HttpServletRequest request = Mvcs.getReq();
        String remoteAddr = request.getHeader("X-Real-IP");
        if (Strings.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("X-Forwarded-For");
        } else if (Strings.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("Proxy-Client-IP");
        } else if (Strings.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("WL-Proxy-Client-IP");
        }
        String ip = remoteAddr != null ? remoteAddr : Strings.sNull(request.getRemoteAddr());
        if (isIPv4Address(ip) || isIPv6Address(ip)) {
            return ip;
        }
        return "";
    }

没有请求,当然没有ip

添加回复
请先登陆
回到顶部