我有一个名为MyServlet的servlet。这个servlet绑定在我的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)
...
}
...
}
答案 0 :(得分:1)
您每次都在致电initAtmosphereHandler
framework.addAtmosphereHandler("/atmosphere/${prefix}/*", rsp).initAtmosphereHandler(sc)
addAtmosphereHandler()
方法只是注册服务。对initAtmosphereHandler
的调用实际上实例化了它们。 javadoc简单地说
初始化
AtmosphereServletProcessor
。
所以第一次循环迭代,你有一个注册服务。在第二次迭代中,您有两个注册的服务。在第三次迭代中,您有三个注册的服务。在每次迭代中,都会实例化所有服务。
摆脱initAtmosphereHandler
调用,并在each
循环之外执行。