本文整理匯總了Java中org.eclipse.jetty.server.Server.setHandler方法的典型用法代碼示例。如果您正苦於以下問題:Java Server.setHandler方法的具體用法?Java Server.setHandler怎麽用?Java Server.setHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.eclipse.jetty.server.Server
的用法示例。
在下文中一共展示了Server.setHandler方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: startWebSocket
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
/**
* start server
*
* @param port
* @param path
* @param handlerClass
*/
public static void startWebSocket(int port, String path, String handlerClass) {
try {
Server server = new Server(port);
HandlerList handlerList = new HandlerList();
ServletContextHandler context = new ServletContextHandler(
ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(new ServletHolder(new Jwservlet(handlerClass)),
path);
handlerList.addHandler(context);
handlerList.addHandler(new DefaultHandler());
server.setHandler(handlerList);
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
LogUtil.LOG("start websocket server error:" + e.getMessage(),
LogLev.ERROR, WebSocketServer.class);
System.exit(1);
}
}
示例2: start
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
private void start() throws Exception {
resourcesExample();
ResourceHandler resourceHandler = new ResourceHandler();
Resource resource = Resource.newClassPathResource(PUBLIC_HTML);
resourceHandler.setBaseResource(resource);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.addServlet(new ServletHolder(new TimerServlet()), "/timer");
Server server = new Server(PORT);
server.setHandler(new HandlerList(resourceHandler, context));
server.start();
server.join();
}
示例3: onEnable
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
@Override
public void onEnable() {
tpsPoller = new TpsPoller(this);
Bukkit.getServer()
.getScheduler()
.scheduleSyncRepeatingTask(this, tpsPoller, 0, TpsPoller.POLL_INTERVAL);
config.addDefault("port", 9225);
config.options().copyDefaults(true);
saveConfig();
int port = config.getInt("port");
server = new Server(port);
server.setHandler(new MetricsController(this));
try {
server.start();
getLogger().info("Started Prometheus metrics endpoint on port " + port);
} catch (Exception e) {
getLogger().severe("Could not start embedded Jetty server");
}
}
示例4: main
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
public static void main(String[] args) {
try {
MergedLogSource src = new MergedLogSource(args);
System.out.println(src);
Server server = new Server(8182);
server.setHandler(new LogServer(src));
server.start();
server.join();
} catch (Exception e) {
// Something is wrong.
e.printStackTrace();
}
}
示例5: createServer
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
private Server createServer() {
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8384);
server.addConnector(connector);
ServletHandler handler = new ServletHandler();
Servlet servlet = new HttpServlet() {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String path = req.getServletPath().substring(1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
resp.getWriter().append(path);
}
};
handler.addServletWithMapping(new ServletHolder(servlet), "/");
server.setHandler(handler);
return server;
}
示例6: startJettyServer
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
/**
* 啟動jetty服務,加載server.war
*/
public static void startJettyServer(String path) throws Exception{
String configPath=path+ File.separator+"conf"+File.separator+"conf.properties";
InputStream is = new FileInputStream(configPath);;
Properties properties =new Properties();
properties.load(is);
is.close();
int serverPort = Integer.parseInt(properties.getProperty("server.port"));
Server server = new Server(serverPort);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
context.setWar(path+"/bin/service.war");
server.setHandler(context);
server.start();
server.join();
}
示例7: main
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
Initiator.init();
Server server = new Server(PORT);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath(CONTEXT_PATH);
server.setHandler(context);
server.setStopAtShutdown(true);
context.addServlet(new ServletHolder(new GetAllLang()), "/get_all_lang");
context.addServlet(new ServletHolder(new Compile()), "/compile");
context.addServlet(new ServletHolder(new Run()), "/run");
server.join();
server.start();
LOGGER.info("Avalon-Executive server is now running at http://127.0.0.1:" + PORT + CONTEXT_PATH);
}
示例8: initSocketServer
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
public void initSocketServer() {
server = new Server();
ServerConnector connector = getServerConnector();
server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
resp.setContentType("text/html; charset=utf-8");
resp.setStatus(HttpServletResponse.SC_OK);
resp.getWriter().println("<div class=\"svg\" style=\"display: flex;align-items: center;justify-content: center;margin-top: 10%;\">\n"
+ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"100\" height=\"100\" viewBox=\"-265 235 30 30\" >\n"
+ " <g fill=\"white\" stroke=\"#8EC343\" stroke-width=\"3\">\n"
+ " <circle cx=\"-250.5\" cy=\"249.5\" r=\"10\"/>\n"
+ " <path d=\"M-257 250l4 3.6 8-8\"/>\n"
+ " </g>\n"
+ " </svg>\n"
+ "</div>");
} catch (Exception ex) {
}
}
}), "/status/*");
server.setHandler(context);
// Add a websocket to a specific path spec
ServletHolder holderEvents = new ServletHolder("ws-events", EventServlet.class);
context.addServlet(holderEvents, "/");
}
示例9: main
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
// assert that PrintWriter.print(String) uses UTF-8, much less verbose than Writer.write(byte[])
if (defaultCharset() != UTF_8) throw new Exception("default charset must be UTF-8 (-Dfile.encoding=UTF-8)");
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(new ServletHolder(new HelloServlet()), "/*");
Server server = new Server(8000);
server.setHandler(handler);
server.start();
server.join();
}
示例10: run
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
public void run() {
try {
System.out.println("Listening on Reqs...");
Server server = new Server(Config.PORT);
// Handler for the voting API
ContextHandler votingContext = new ContextHandler();
votingContext.setContextPath("/vote");
votingContext.setHandler(new VoteHandler());
// Handler for the stats API
ContextHandler statContext = new ContextHandler();
statContext.setContextPath("/stats");
statContext.setHandler(new StatsHandler());
// summing all the Handlers up to one
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[] { votingContext, statContext});
server.setHandler(contexts);
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
}
示例11: startJetty
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
private void startJetty(int port) throws Exception {
Server server = new Server(port);
WebApplicationContext webApplicationContext = createWebApplicationContext();
ServletContextHandler handler = createHandlerWith(webApplicationContext);
server.setHandler(handler);
server.start();
server.join();
}
示例12: WebUIApp
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
public WebUIApp() throws IOException {
server = new Server();
AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
springContext.setClassLoader(this.getClass().getClassLoader());
springContext.setConfigLocation("com.sst.burpextender.proxyhistory.webui.springmvc.config");
DispatcherServlet dispatcherServlet = new DispatcherServlet(springContext);
ServletHolder springServletHolder = new ServletHolder("mvc-dispatcher", dispatcherServlet);
// ref: https://github.com/bkielczewski/example-spring-mvc-jetty
// ref: https://github.com/fernandospr/spring-jetty-example
ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setErrorHandler(null);
contextHandler.setContextPath("/");
contextHandler.addServlet(springServletHolder, "/*");
contextHandler.addEventListener(new ContextLoaderListener(springContext));
contextHandler.setResourceBase(new ClassPathResource("/webui", WebUIApp.class).getURI().toString());
/* NOTE: Burp Extender において、 burp.BurpExtender については jar をロードしたclass loader內で実行される。
* 一方、Tabなど獨自のUIを追加した場合は、UIから呼ばれるコードはBurp自身のclass loader內で実行される。
* つまり、Webアプリのstart/stopをBurp上に追加したUIからそのまま = Swingのスレッド內から呼び出す場合、
* その一連の過程で使われるclass loaderはburp自身のものであり、
* 「Burp Extender の jar 內のclassを見つけることができない。」
* # 実際に見てみると、Thread.currentThread().getContextClassLoader() が sun.misc.Launcher$AppClassLoader となる.
*
* その結果として、特に何の手當もしなかった場合、以下のような様々な、予期せぬclass loading関連の例外に遭遇する。
* - Spring で setConfigLocation() で指定したパッケージがスキャンされない。
* - Spring で @ComponentScan(basePackages = xxx) で指定したパッケージがスキャンされない。
* ==> 結果として Spring MVC の初期化時に "No annotated classes found for specified class/package" のログが出力される。
* - Spring + Thymeleaf 內部でのSpring EL式の生成で class not found 例外発生
*
* そこで、Jetty側のレベルでclass loaderをカスタマイズする。
* 本クラスのインスタンスをロードした class loader であれば、Burp Extender の jar をロードしているので、それを使うようにする。
* これにより、Spring の annotation class のスキャンや、Spring MVC 內部で発生する様々な class loading が本來の形で動作するようになる。
*/
contextHandler.setClassLoader(this.getClass().getClassLoader());
CharacterEncodingFilter utf8Filter = new CharacterEncodingFilter();
utf8Filter.setEncoding("UTF-8");
utf8Filter.setForceEncoding(true);
FilterHolder filterHolder = new FilterHolder(utf8Filter);
EnumSet<DispatcherType> allDispatcher = EnumSet.of(
DispatcherType.ASYNC,
DispatcherType.ERROR,
DispatcherType.FORWARD,
DispatcherType.INCLUDE,
DispatcherType.REQUEST);
contextHandler.addFilter(filterHolder, "/*", allDispatcher);
server.setHandler(contextHandler);
}
示例13: initJettyServer
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
private void initJettyServer() {
InetSocketAddress address = new InetSocketAddress(config.getHost(), config.getPort());
jettyServer = new Server(address);
handler = new ServletContextHandler();
handler.setContextPath(config.getContextPath());
handler.setClassLoader(new JbootServerClassloader(JettyServer.class.getClassLoader()));
handler.setResourceBase(getRootClassPath());
JbootShiroConfig shiroConfig = Jboot.config(JbootShiroConfig.class);
if (shiroConfig.isConfigOK()) {
handler.addEventListener(new EnvironmentLoaderListener());
handler.addFilter(ShiroFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
}
//JFinal
FilterHolder jfinalFilter = handler.addFilter(JFinalFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
jfinalFilter.setInitParameter("configClass", Jboot.me().getJbootConfig().getJfinalConfig());
JbootHystrixConfig hystrixConfig = Jboot.config(JbootHystrixConfig.class);
if (StringUtils.isNotBlank(hystrixConfig.getUrl())) {
handler.addServlet(HystrixMetricsStreamServlet.class, hystrixConfig.getUrl());
}
JbootMetricConfig metricsConfig = Jboot.config(JbootMetricConfig.class);
if (StringUtils.isNotBlank(metricsConfig.getUrl())) {
handler.addEventListener(new JbootMetricServletContextListener());
handler.addEventListener(new JbootHealthCheckServletContextListener());
handler.addServlet(AdminServlet.class, metricsConfig.getUrl());
}
io.jboot.server.Servlets jbootServlets = new io.jboot.server.Servlets();
ContextListeners listeners = new ContextListeners();
JbootAppListenerManager.me().onJbootDeploy(jbootServlets, listeners);
for (Map.Entry<String, io.jboot.server.Servlets.ServletInfo> entry : jbootServlets.getServlets().entrySet()) {
for (String path : entry.getValue().getUrlMapping()) {
handler.addServlet(entry.getValue().getServletClass(), path);
}
}
for (Class<? extends ServletContextListener> listenerClass : listeners.getListeners()) {
handler.addEventListener(ClassKits.newInstance(listenerClass));
}
jettyServer.setHandler(handler);
}
示例14: start
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
public void start(boolean devMode, int port) throws Exception {
Server server = new Server(new QueuedThreadPool(500));
WebAppContext appContext = new WebAppContext();
String resourceBasePath = "";
//開發者模式
if (devMode) {
String artifact = MavenUtils.get(Thread.currentThread().getContextClassLoader()).getArtifactId();
resourceBasePath = artifact + "/src/main/webapp";
}
appContext.setDescriptor(resourceBasePath + "WEB-INF/web.xml");
appContext.setResourceBase(resourceBasePath);
appContext.setExtractWAR(true);
//init param
appContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
if (CommonUtils.isWindowOs()) {
appContext.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
}
//for jsp support
appContext.addBean(new JettyJspParser(appContext));
appContext.addServlet(JettyJspServlet.class, "*.jsp");
appContext.setContextPath("/");
appContext.getServletContext().setExtendedListenerTypes(true);
appContext.setParentLoaderPriority(true);
appContext.setThrowUnavailableOnStartupException(true);
appContext.setConfigurationDiscovered(true);
appContext.setClassLoader(Thread.currentThread().getContextClassLoader());
ServerConnector connector = new ServerConnector(server);
connector.setHost("0.0.0.0");
connector.setPort(port);
server.setConnectors(new Connector[]{connector});
server.setAttribute("org.eclipse.jetty.server.Request.maxFormContentSize", 1024 * 1024 * 1024);
server.setDumpAfterStart(false);
server.setDumpBeforeStop(false);
server.setStopAtShutdown(true);
server.setHandler(appContext);
logger.info("[opencron] JettyLauncher starting...");
server.start();
}
示例15: main
import org.eclipse.jetty.server.Server; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
// Check the command line arguments.
if (args.length == 0) {
printErrorMessageAndDie();
}
// Load all the properties.
Properties props = new Properties();
try (InputStream propStream = new FileInputStream(args[0])) {
props.load(propStream);
}
int port = 9090;
if (args.length > 1) {
try {
port = Integer.parseInt(args[1]);
} catch (Exception e) {
printErrorMessageAndDie();
}
}
KafkaCruiseControl kafkaCruiseControl = new KafkaCruiseControl(new KafkaCruiseControlConfig(props));
Server server = new Server(port);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
// Placeholder for any static content
DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderWebapp = new ServletHolder("default", defaultServlet);
holderWebapp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
holderWebapp.setInitParameter("resourceBase", "./cruise-control-ui/dist/");
context.addServlet(holderWebapp, "/*");
// Kafka Cruise Control servlet data
KafkaCruiseControlServlet kafkaCruiseControlServlet = new KafkaCruiseControlServlet(kafkaCruiseControl);
ServletHolder servletHolder = new ServletHolder(kafkaCruiseControlServlet);
context.addServlet(servletHolder, "/kafkacruisecontrol/*");
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
kafkaCruiseControl.shutdown();
}
});
kafkaCruiseControl.startUp();
server.start();
System.out.println("Application directory: " + System.getProperty("user.dir"));
System.out.println("Kafka Cruise Control started.");
}