NutzBook - 为正式开发做准备

NutzBook - 为正式开发做准备

Tags: Nutz

摘要

NutzBook - 为正式开发做准备

下载更多jar

Shiro

shiro-all-1.2.3.jar
slf4j-api-1.7.12.jar
slf4j-log4j12-1.7.12.jar
commons-beanutils-1.9.2.jar
commons-logging-1.2.jar
nutz-integration-shiro-1.b.53.r2.jar

Quartz

quartz-2.2.1.jar
quartz-jobs-2.2.1.jar
nutz-integration-quartz-1.b.53.r3.jar

jstl

javax.servlet.jsp.jstl-api-1.2.1.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-spec-1.2.5.jar
taglibs-standard-jstlel-1.2.5.jar

改进dao.js

把数据库配置信息提取到db.properties中。
dao.js里面再用cnf对象进行引用。

自定义NutFilter

package net.javablog.mvc;

import org.nutz.mvc.NutFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class NutzBookNutFilter extends NutFilter {

    protected Set<String> prefixs = new HashSet<String>();


    public void init(FilterConfig conf) throws ServletException {
        super.init(conf);
        prefixs.add(conf.getServletContext().getContextPath() + "/druid/");
        prefixs.add(conf.getServletContext().getContextPath() + "/rs/");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        if (req instanceof HttpServletRequest) {
            String uri = ((HttpServletRequest) req).getRequestURI();
            for (String prefix : prefixs) {
                if (uri.startsWith(prefix)) {
                    chain.doFilter(req, resp);
                    return;
                }
            }
        }
        super.doFilter(req, resp, chain);
    }
}

注意引用的servlet是2.5

    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>

修改web.xml中的filter。
org.nutz.mvc.NutFilter替换为net.javablog.mvc.NutzBookNutFilter

配置druid监控

配置dao.js web.xml

打开dao.js, 为DruidDataSource加入监控选项, 分别是filters和connectionProperties
filters是druid定义的一些过滤器,mergeStat是带合并的sql状态过滤器, 而connectionProperties配置中的2000代表如果sql执行超过2秒,就输出日志

打开web.xml, 在nutz的filter之前, 加入Web监控的配置

<filter>
        <filter-name>DruidWebStatFilter</filter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
        <init-param>
            <param-name>exclusions</param-name>
            <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,/rs/*</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>DruidWebStatFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

在welcome-file之前,再加入

<servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>

查看druid监控页面

访问页面 http://127.0.0.1:8080/druid/index.html

查看web统计 http://127.0.0.1:8080/druid/webapp.html

查询数据库操作的统计信息 http://127.0.0.1:8080/druid/sql.html

配置动作链

resources中新建一个文件夹mvc,新增一个配置文件叫nutzbook-mvc-chain.js, 内容如下

var chain = {
    "default": {
        "ps": [
            "net.javablog.mvc.LogTimeProcessor",
            "org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor",
            "org.nutz.mvc.impl.processor.EncodingProcessor",
            "org.nutz.mvc.impl.processor.ModuleProcessor",
            "org.nutz.mvc.impl.processor.ActionFiltersProcessor",
            "org.nutz.mvc.impl.processor.AdaptorProcessor",
            "org.nutz.mvc.impl.processor.MethodInvokeProcessor",
            "org.nutz.mvc.impl.processor.ViewProcessor"
        ],
        "error": 'org.nutz.mvc.impl.processor.FailProcessor'
    }
};

LogTimeProcessor的作用就是记录每个请求的耗时,对于分析服务性能很有帮助

package net.javablog.mvc;

import org.nutz.lang.Stopwatch;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.impl.processor.AbstractProcessor;

import javax.servlet.http.HttpServletRequest;

public class LogTimeProcessor extends AbstractProcessor {

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

    public LogTimeProcessor() {
    }

    @Override
    public void process(ActionContext ac) throws Throwable {
        Stopwatch sw = Stopwatch.begin();
        try {
            doNext(ac);
        } finally {
            sw.stop();
            if (log.isDebugEnabled()) {
                HttpServletRequest req = ac.getRequest();
                log.debugf("[%4s]URI=%s %sms", req.getMethod(), req.getRequestURI(), sw.getDuration());
            }
        }
    }

}

重启jetty,尝试登陆登出

加入Shiro

resources文件夹下新建文件shiro.ini,内容如下

[main]

[urls]
/* = anon

web.xml中所有filter的前面加上如下listener

<listener>
        <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>ShiroFilter</filter-name>
        <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>ShiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

启动tomcat, 可以看到多输出了一些关于shiro的log,这些log比nutz早

加入Quartz

resources文件夹下新建文件quartz.properties,内容如下

org.quartz.scheduler.instanceName = NutzbookScheduler 
org.quartz.threadPool.threadCount = 3 
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.scheduler.skipUpdateCheck=true

打开MainModule,修改IocBy为

@IocBy(type = ComboIocProvider.class, args = {"*js", "ioc/",
        "*anno", "net.javablog",
        "*tx",
        "*org.nutz.integration.quartz.QuartzIocLoader"})

mainSetup init方法中最后新增
ioc.get(NutQuartzCronJobFactory.class);

重启jetty,可以看到如下日志

2016-05-16 16:21:02,867 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:208) DEBUG -      >> Make...'scheduler'<>
2016-05-16 16:21:02,867 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'scheduler' to [app] 
2016-05-16 16:21:02,893 org.quartz.impl.StdSchedulerFactory.instantiate(StdSchedulerFactory.java:1184) INFO  - Using default implementation for ThreadExecutor
2016-05-16 16:21:02,908 org.quartz.core.SchedulerSignalerImpl.<init>(SchedulerSignalerImpl.java:61) INFO  - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2016-05-16 16:21:02,908 org.quartz.core.QuartzScheduler.<init>(QuartzScheduler.java:240) INFO  - Quartz Scheduler v.2.2.1 created.
2016-05-16 16:21:02,909 org.quartz.simpl.RAMJobStore.initialize(RAMJobStore.java:155) INFO  - RAMJobStore initialized.
2016-05-16 16:21:02,910 org.quartz.core.QuartzScheduler.initialize(QuartzScheduler.java:305) INFO  - Scheduler meta-data: Quartz Scheduler (v2.2.1) 'NutzbookScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

2016-05-16 16:21:02,910 org.quartz.impl.StdSchedulerFactory.instantiate(StdSchedulerFactory.java:1339) INFO  - Quartz scheduler 'NutzbookScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
2016-05-16 16:21:02,910 org.quartz.impl.StdSchedulerFactory.instantiate(StdSchedulerFactory.java:1343) INFO  - Quartz scheduler version: 2.2.1
2016-05-16 16:21:02,910 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'jobFactory'<>
2016-05-16 16:21:02,911 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:178) DEBUG -      >> Load definition name=jobFactory
2016-05-16 16:21:02,911 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) DEBUG - Loading define for name=jobFactory
2016-05-16 16:21:02,911 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) DEBUG - Found IocObject(jobFactory) in QuartzIocLoader@1924935772
2016-05-16 16:21:02,912 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:208) DEBUG -      >> Make...'jobFactory'<>
2016-05-16 16:21:02,913 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class org.nutz.integration.quartz.NutQuartzJobFactory without AOP
2016-05-16 16:21:02,913 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'jobFactory' to [app]

新增公共类

BasePojo 公用字段 protected Date createTime; protected Date updateTime;

BaseModule 公用字段 @Inject protected Dao dao;

Toolkit 3DES加密的封装,kv字符串转换,密码加密hash