main start Jetty Server web project

Keywords: Jetty Spring Eclipse xml

main start Jetty Server mode

1, web engineering starts jetty server through main

1.JettyServerLauncher

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;


public class JettyServerLauncher {
	
	private static final int DEFAULT_PORT = 8080;
	private static final String DEFAULT_WEBAPP = "WebRoot";
	private static final String DEFAULT_WEB = "WebRoot/WEB-INF/web.xml";
	private static final String DEFAULT_PROJECT = "ABMP";
	private static Server server;
	
    public static void main(String[] args) throws Exception {
        JettyServerLauncher.startJetty(getPortFromArgs(args));
    }
        
    private static int getPortFromArgs(String[] args) {
        if (args.length > 0) {
            try {
                return Integer.valueOf(args[0]);
            } catch (NumberFormatException ignore) {
            	System.out.println(ignore);
            }
        }
        return DEFAULT_PORT;
    }

    private static void startJetty(int port) throws Exception {       
        server = new Server(port);
        server.setHandler(getWebAppContext());
        server.start();
        server.join();
    }
    
    public static void stopJetty() throws Exception{
    	server.stop();
    }
    
    private static WebAppContext getWebAppContext(){
    	WebAppContext context = new WebAppContext();  
        context.setDescriptor(DEFAULT_WEB);     
        context.setResourceBase(DEFAULT_WEBAPP);        
        context.setContextPath(DEFAULT_PROJECT);  
        context.setParentLoaderPriority(true);  
        return context;
    }
    
}


2.Jetty meaning of starting server.join

If the server doesn't get up, the join() function rides to block the threads. Here, the join() function essentially calls the jetty's Thread pool. (this is the same as the join function in Thread.)


If there is no join function, the jetty server can start or run normally, because jetty is relatively small and starts very fast.


However, if your application is heavy, calling the join function can ensure that your server is really up.





2, spring integrated jersey mode


public static void launchSpringJerseyServer(int port, String contextPath, Map<String, String> paramMap) {
		
		ServletHolder servletHolder = new ServletHolder(SpringServlet.class);
		/**
		 * Load spring profile
		 */
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-all.xml");

		SpringUtils.setApplicationContext(ctx);// Save Spring context objects to static variables
				
		servletHolder.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
				"com.sun.jersey.api.core.PackagesResourceConfig");
		/**
		 * Set the package where the processor (servlet) is located. If you do not set it, you will not be able to find the processed servlet
		 */
		servletHolder.setInitParameter("com.sun.jersey.config.property.packages",
				"org.vimtang.ws.rs.server.jersey.service");
		/**
		 * Make the return type (json) set by jersey valid
		 */
		servletHolder.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
		
		servletHolder.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters",
				"org.vimtang.ws.rs.server.jersey.filter.CORSFilter");
		
		/**
		 * Set ApplicationContext for jersey's ServletContextHandler
		 */
		ServletContextHandler contextHandler = new ServletContextHandler();
		contextHandler.setInitParameter("spring.profiles.active",
				"dev");
		contextHandler.setInitParameter("spring.profiles.default",
				"dev");
		contextHandler.setInitParameter("spring.liveBeansView.mbeanDomain",
				"dev");
		contextHandler.setContextPath(contextPath);		
		
		contextHandler.setClassLoader(ctx.getClassLoader());
		XmlWebApplicationContext xmlWebAppContext = new XmlWebApplicationContext();
		xmlWebAppContext.setParent(ctx);
		xmlWebAppContext.setConfigLocation("");
		xmlWebAppContext.setServletContext(contextHandler.getServletContext());
		xmlWebAppContext.refresh();
		contextHandler.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, xmlWebAppContext);
		/**
		 * Set the access path corresponding to the servlet
		 */
		String webServiceCtxRegex = paramMap.get(_CALLER_WS_CONTEXT);
		if (null == webServiceCtxRegex) {
			webServiceCtxRegex = "/ws/*";
		}
		contextHandler.addServlet(servletHolder, webServiceCtxRegex);
		HandlerList handlers = new HandlerList();
		handlers.addHandler(contextHandler);

		Server server = new Server();
	
		SelectChannelConnector connector0 = new SelectChannelConnector();   
		connector0.setPort(port);
		connector0.setMaxIdleTime(30000);   
		connector0.setRequestHeaderSize(1024000); 		
		connector0.setThreadPool(new QueuedThreadPool(1024)); 
		
		server.setConnectors(new Connector[]{connector0});
		server.setHandler(handlers);
		try {
			server.start();
			server.join();			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {		
		try {
			Map<String, String> props = fetchProperties();			
			int port = 82;			
			String contextPath = "/iqm";				
			launchSpringJerseyServer(port, contextPath, props);			
		} catch(Exception ex) {
			logger.error("server launching failed", ex);
		}
	}



Posted by orionellis on Fri, 01 May 2020 14:22:37 -0700