我有一个名为 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)
...
}
...
}