java - Servlet 被多次初始化?

原文 标签 java spring servlets grails grails-2.0

我有一个名为 MyServlet 的 servlet。这个 servlet 绑定(bind)在我的 web.xml 文件中。这个 servlet 正在遍历我的 Grails 服务并负责以 ListenerService 结尾的服务。如果是这样,它会为此服务初始化一个新的 Servlet。

我有以下监听器服务:

ChatListenerService
NotificationListenerService
ThirdListenerService

这意味着我应该得到三个由 MyServlet 生成的 Servlet。当您查看输出时,您会看到第一个 Servlet 生成了三次,第二个生成了两次,第三个 Service 生成了一次。

这是我得到的输出:
2013-10-25 13:05:50,460 [localhost-startStop-1] INFO  cpr.AtmosphereServlet  - AtmosphereServlet with native support for Tomcat 6/7 and JBossWeb Installed.
>>>>>>>>>>>>>>> been: test.ChatListenerService@41028653
2013-10-25 13:05:50,577 [localhost-startStop-1] INFO  handler.ReflectorServletProcessor  - Installing Servlet test.ChatListenerService
2013-10-25 13:05:50,577 [localhost-startStop-1] INFO  test.MyServlet  - registered chatListener as handler for /atmosphere/chat/*
>>>>>>>>>>>>>>> been: test.NotificationListenerService@70b005e0
2013-10-25 13:05:50,582 [localhost-startStop-1] INFO  handler.ReflectorServletProcessor  - Installing Servlet test.NotificationListenerService
2013-10-25 13:05:50,582 [localhost-startStop-1] INFO  handler.ReflectorServletProcessor  - Installing Servlet test.ChatListenerService
2013-10-25 13:05:50,582 [localhost-startStop-1] INFO  test.MyServlet  - registered notificationListener as handler for /atmosphere/notification/*
>>>>>>>>>>>>>>> been: test.ThirdListenerService@4cba9be9
2013-10-25 13:05:50,587 [localhost-startStop-1] INFO  handler.ReflectorServletProcessor  - Installing Servlet test.NotificationListenerService
2013-10-25 13:05:50,587 [localhost-startStop-1] INFO  handler.ReflectorServletProcessor  - Installing Servlet test.ChatListenerService
2013-10-25 13:05:50,587 [localhost-startStop-1] INFO  handler.ReflectorServletProcessor  - Installing Servlet test.ThirdListenerService
2013-10-25 13:05:50,587 [localhost-startStop-1] INFO  test.MyServlet  - registered pedigreeListener as handler for /atmosphere/third/*
2013-10-25 13:05:50,587 [localhost-startStop-1] INFO  test.MyServlet  - Initialized MyServlet

这里出了什么问题?因为我只对每个服务进行一次迭代,所以我看不出为什么会多次生成服务。我必须解决什么问题?

我的 web.xml:
<servlet>
    <description>MyServlet</description>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>test.MyServlet</servlet-class>
    <load-on-startup>0</load-on-startup>

    <!-- If you want to use Servlet 3.0 -->
    <async-supported>true</async-supported>

    <init-param>
        <param-name>org.atmosphere.useNative</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.cpr.AtmosphereInterceptor</param-name>
        <param-value>org.atmosphere.interceptor.AtmosphereResourceLifecycleInterceptor,org.atmosphere.interceptor.HeartbeatInterceptor,org.atmosphere.client.TrackMessageSizeInterceptor</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.cpr.broadcaster.shareableThreadPool</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
           <param-name>org.atmosphere.useWebSocketAndServlet3</param-name>
           <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.cpr.Broadcaster.writeTimeout</param-name>
        <param-value>30000</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.cpr.sessionSupport</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.cpr.broadcasterCacheClass</param-name>
        <param-value>org.atmosphere.cache.UUIDBroadcasterCache</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/atmosphere/*</url-pattern>
</servlet-mapping>

我的 MyService 类:
class MyServlet extends MeteorServlet {

    @Override
     void init(ServletConfig sc) throws ServletException {
         super.init(sc)

        def webApplicationContext = WebApplicationContextUtils.getWebApplicationContext sc.servletContext

        def grailsApplication = webApplicationContext.getBean("grailsApplication")

        def services = grailsApplication.getArtefacts "Service"

        def suffix = "Listener"

        services.each { serviceClass ->
            def serviceName = serviceClass.logicalPropertyName
            if(serviceName.endsWith(suffix)) {
                def prefix = serviceName - suffix

                def bean = webApplicationContext.getBean(serviceClass.clazz)

                def servlet = serviceClass.clazz

                String servletClass = bean.class.getName()
                ReflectorServletProcessor rsp = new ReflectorServletProcessor(bean)
                rsp.setServletClassName(servletClass)

                framework.addAtmosphereHandler("/atmosphere/${prefix}/*", rsp).initAtmosphereHandler(sc)        

                log.info "registered $serviceName as handler for /atmosphere/${prefix}/*"
            }
        }

        log.info "Initialized MyServlet"

     }  

我的 ChatListenerService(另一个看起来相似):
class ChatListenerService extends HttpServlet {


    static transactional = false

        @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
          Meteor meteor = Meteor.build(request)
          ...
        }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
          Meteor meteor = Meteor.build(request)
          ...
        }
...
}

最佳答案

您调用initAtmosphereHandler每次

framework.addAtmosphereHandler("/atmosphere/${prefix}/*", rsp).initAtmosphereHandler(sc)        

addAtmosphereHandler() 方法只是注册服务。调用 initAtmosphereHandler 实际上实例化它们。 javadoc 只是说

Initialize AtmosphereServletProcessor.



所以第一次循环迭代,你有一个注册的服务。在第二次迭代中,您有两个已注册的服务。在第三次迭代中,您拥有三个已注册的服务。在每次迭代中,所有服务都被实例化。

摆脱initAtmosphereHandlereach 之外调用电话并进行操作环形。

关于java - Servlet 被多次初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19591752/

相关文章:

java - 如何解决GPS服务程序中的空指针异常?

java - restTemplate 交换经常导致 400 错误

java - 如何在 Spring 将其反序列化为 POJO 之前访问原始请求有效负载?

java - Liferay 中不同的 "request"隐式对象

java - org.springframework.web.servlet.PageNotFound noHandlerFound没有为HTTP请求找到映射

java - Android JSON通讯

java - Javax.sound.midi学习代码无法在MacOS X上运行

java - 使用spring security进行身份验证后如何根据角色进行重定向

java - SingleConnectionDataSource 连接在大约 40 分钟不活动后关闭

java - 如何将 FileItem 附加到 HTTP POST 请求?