Restful后台系统搭建(三)

本章主要讲解服务的启动以及使用jmeter简单测试。


先展示本章所涉及的几个包+文件分布:



分别讲解每一个类的目的

FrameworkException:

每个框架都应该有自己的异常子类,以便传输或者转换一些异常,通过ErrorCode来定义自己的错误码,方便接口返回时展示。


FrameworkErrorCode:

错误码表。


Main:

程序入口。


ServiceStarter:

服务启动器的接口,子类需要实现其callback方法。


ServiceStarterRegister:

服务启动器的注册类,所有需要启动的服务将实现ServiceStarter接口,并在该类中注册。


JerseyServerStarter:

负责启动Jetty服务并注册Jersey。


TestService:

测试用的服务接口,提供对外访问的url入口。



下面是每个类的代码实现:

FrameworkException:

/**
 * 2017年4月29日 黑苹果
 */
package com.black.apple.framework.exception;

import org.apache.commons.lang3.StringUtils;

import com.google.common.base.Optional;

/**
 * @author 黑苹果 2017年4月29日
 */
public class FrameworkException extends Exception
{
    public FrameworkErrorCode errorCode;
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -1533750106989537039L;

    public FrameworkException(FrameworkErrorCode errorCode)
    {
        super();
        this.errorCode = errorCode;
    }

    public FrameworkException(FrameworkErrorCode errorCode, Throwable t)
    {
        super(t);
        this.errorCode = errorCode;
    }

    public String getErrorCode()
    {
        return Optional.fromNullable(errorCode).isPresent() ? errorCode.toString() : StringUtils.EMPTY;
    }
}

FrameworkErrorCode:

/**
 * 2017年4月29日 黑苹果
 */
package com.black.apple.framework.exception;

/**
 * @author 黑苹果 2017年4月29日
 */
public enum FrameworkErrorCode
{
    /**
     * 成功
     */
    OK(0),

    /**
     * Jersey服务启动失败
     */
    JERSEY_SERVICE_START_FAILED(1),

    /**
     * 未知异常
     */
    UNKNOW_EXCEPTION(9999);

    private int errorCode;

    /**
     * 错误码范围为:10000000~10009999
     */
    private static final int ERROR_CODE_PREFIX = 1000_0000;

    private FrameworkErrorCode(int errorCode)
    {
        this.errorCode = ERROR_CODE_PREFIX + errorCode;
    }

    public String toString()
    {
        return String.valueOf(errorCode);
    }
}

Main:

/**
 * 2017年4月29日 黑苹果
 */
package com.black.apple.framework.main;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.black.apple.framework.exception.FrameworkException;
import com.google.common.base.Optional;

/**
 * @author 黑苹果 2017年4月29日
 */
public class Main
{
    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        List<ServiceStarter> services = ServiceStarterRegister.getServices();
        for (ServiceStarter serviceStarter : services)
        {
            serviceStart(serviceStarter);
        }
    }

    private static void serviceStart(ServiceStarter serviceStarter)
    {
        if (!Optional.fromNullable(serviceStarter).isPresent())
        {
            return;
        }
        try
        {
            serviceStarter.callback();
        }
        catch (FrameworkException e)
        {
            LOGGER.error("Error in service starter when do callback.", e);
        }
        catch (Exception e)
        {
            LOGGER.error("Error in service starter when do callback.", e);
        }
    }

}


ServiceStarter:

/**
 * 2017年4月29日 黑苹果
 */
package com.black.apple.framework.main;

import com.black.apple.framework.exception.FrameworkException;

/**
 * @author 黑苹果 2017年4月29日
 */
public interface ServiceStarter
{
    /**
     * 服务启动器回调方法
     */
    public void callback() throws FrameworkException;
}


ServiceStarterRegister:

/**
 * 2017年4月29日 黑苹果
 */
package com.black.apple.framework.main;

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

import com.black.apple.framework.jersey.rest.service.JerseyServerStarter;

/**
 * @author 黑苹果 2017年4月29日
 */
public final class ServiceStarterRegister
{
    private static final ServiceStarterRegister INSTANCE = new ServiceStarterRegister();

    private List<ServiceStarter> services = new ArrayList<ServiceStarter>();

    private ServiceStarterRegister()
    {
        registerService();
    }

    private void registerService()
    {
        // TODO 添加Service
        services.add(new JerseyServerStarter());
    }

    private static ServiceStarterRegister getInstance()
    {
        return INSTANCE;
    }

    public static List<ServiceStarter> getServices()
    {
        return getInstance().services;
    }

}

JerseyServerStarter:

/**
 * 2017年4月29日 黑苹果
 */
package com.black.apple.framework.jersey.rest.service;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.black.apple.framework.exception.FrameworkErrorCode;
import com.black.apple.framework.exception.FrameworkException;
import com.black.apple.framework.main.ServiceStarter;
import com.sun.jersey.spi.container.servlet.ServletContainer;

/**
 * @author 黑苹果 2017年4月29日
 */
public class JerseyServerStarter implements ServiceStarter
{
    private static final Logger LOGGER = LoggerFactory.getLogger(JerseyServerStarter.class);

    private static final int PORT = 8687;

    /*
     * (non-Javadoc)
     * 
     * @see com.black.apple.framework.main.ServiceStarter#callback()
     */
    @Override
    public void callback() throws FrameworkException
    {
        Server server = new Server(PORT);
        ServletHolder servlet = new ServletHolder(ServletContainer.class);
        servlet.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
                "com.sun.jersey.api.core.PackagesResourceConfig");
        servlet.setInitParameter("com.sun.jersey.config.property.packages",
                "com.black.apple.framework.jersey.rest.service");
        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        handler.setContextPath("/");
        handler.addServlet(servlet, "/*");
        server.setHandler(handler);
        try
        {
            server.start();
        }
        catch (Exception e)
        {
            LOGGER.error("Error in start jersey server.", e);
            throw new FrameworkException(FrameworkErrorCode.JERSEY_SERVICE_START_FAILED, e);
        }
    }

}

TestService:

/**
 * 2017年4月29日 黑苹果
 */
package com.black.apple.framework.jersey.rest.service;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * @author 黑苹果 2017年4月29日
 */

@Path("framework")
public class TestService
{
    @POST
    @Path("test")
    @Produces(MediaType.APPLICATION_JSON)
    public String test(String content)
    {
        return "test" + content;
    }
}



Jmeter测试:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值