RESTLET GAE ECLIPSE初始化方法 [英] RESTLET GAE ECLIPSE initializate method

查看:192
本文介绍了RESTLET GAE ECLIPSE初始化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Restlet,GAE和Eclipse开发一个应用程序。 Oks,我得到了:



public class MainRestletApplication extends Application {

  public MainRestletApplication()
{
// init code?
}


@Override
public Restlet createInboundRoot(){
路由器路由器=新路由器(getContext());

router.attach(/ v1 / mainstatus,MainStatus.class);
router.attach(/ v1 / game / {id} / result,GameResult.class);

返回路由器;
}

}



这个:

 < servlet> 
< servlet-name> RestletServlet< / servlet-name>
< servlet-class> org.restlet.ext.servlet.ServerServlet< / servlet-class>
< init-param>
< param-name> org.restlet.application< / param-name>
< param-value> com.example.MainRestletApplication
< / param-value>
< / init-param>
< / servlet>

<! - 捕获所有请求 - >
< servlet-mapping>
< servlet-name> RestletServlet< / servlet-name>
< url-pattern> / *< / url-pattern>
< / servlet-mapping>






嗯,在哪里可以把一个方法放到init Web服务,ergo,一些代码只有当应用程序启动(一次)才能初始化一些数据,而不是当第一个呼叫进入时。



谢谢

解决方案

您是否使用Restlet或者不使用AppEngine,您可以在web.xml中的任何servlet环境中设置上下文侦听器,如下所示: / p>

 < listener> 
< listener-class>
example.ServletContextExample
< / listener-class>
< / listener>

并实现此;

  public class ServletContextExample实现ServletContextListener {
ServletContext context;
public void contextInitialized(ServletContextEvent contextEvent){
System.out.println(Context Created);
context = contextEvent.getServletContext();
//将变量设置为servlet上下文
context.setAttribute(TEST,TEST_VALUE);
}
public void contextDestroyed(ServletContextEvent contextEvent){
context = contextEvent.getServletContext();
System.out.println(Context Destroyed);
}
}

当您的GAE上的实例启动时,将在执行呼叫之前执行;另一方面,一个新的实例很可能是因为必须处理一个调用而启动的。鸡和鸡蛋故事...


I'm developing an application with Restlet, GAE and Eclipse. Oks, I got this:

public class MainRestletApplication extends Application {

public MainRestletApplication()
{
//init code?
} 


@Override
public Restlet createInboundRoot() {
    Router router = new Router(getContext());

    router.attach("/v1/mainstatus",MainStatus.class);
    router.attach("/v1/game/{id}/result",GameResult.class);

    return router;
}

}

and this:

<servlet>
    <servlet-name>RestletServlet</servlet-name>
    <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    <init-param>
        <param-name>org.restlet.application</param-name>
        <param-value>com.example.MainRestletApplication
        </param-value>
    </init-param>
</servlet>

<!-- Catch all requests -->
<servlet-mapping>
    <servlet-name>RestletServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>


Well, where can I put a method to init the Web Service, ergo, some code to init some data only when the app starts (one time), not when the first call coming.

Thanks

解决方案

wether you use Restlet or not, AppEngine or not, you can setup a context listener in any servlet environment in your web.xml like this :

<listener>
    <listener-class>
        example.ServletContextExample
    </listener-class>
</listener>

And implement this ;

public class ServletContextExample implements ServletContextListener{
    ServletContext context;
    public void contextInitialized(ServletContextEvent contextEvent) {
        System.out.println("Context Created");
        context = contextEvent.getServletContext();
        // set variable to servlet context
        context.setAttribute("TEST", "TEST_VALUE");
    }
    public void contextDestroyed(ServletContextEvent contextEvent) {
        context = contextEvent.getServletContext();
        System.out.println("Context Destroyed");
    }
}

When your instance on GAE is started, the init code will be executed, before a call is processed; on the other hand, a new instance is most likely started because a call must be processed. chicken and egg story...

这篇关于RESTLET GAE ECLIPSE初始化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆