2013-12-17 32 views
0

我想在Spring MVC項目中使用針對所有瀏覽器的JDK 1.6提供推送通知。我跟着this post,最後決定去Atmosphere。 對於服務器發送的事件,我的服務器控制器(source):整合大氣與Spring MVC

@Controller 
public class AtmosphereController { 

    @RequestMapping(value="/getTime", method=RequestMethod.GET) 
    @ResponseBody 
    public void websockets(final AtmosphereResource atmosphereResource) { 

     final HttpServletRequest request = atmosphereResource.getRequest(); 
     final HttpServletResponse response = atmosphereResource.getResponse(); 

     atmosphereResource.suspend(); 

     final Broadcaster bc = atmosphereResource.getBroadcaster(); 
     bc.scheduleFixedBroadcast(new Callable<String>() { 

      public String call() throws Exception { 

       return (new Date()).toString(); 
      } 
     }, 10, TimeUnit.SECONDS); 
    } 
} 

在運行時,我得到了org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.atmosphere.cpr.AtmosphereResource]: Specified class is an interface。 在解決這個問題時,我得到了this relevant post。我將此添加到我的調度員servlet.xml中:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name="messageConverters"> 
      <list> 
       <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
      </list> 
     </property> 
    </bean> 

<mvc:annotation-driven> 
    <mvc:argument-resolvers> 
     <bean id= "atmosphereResource" 
      class="org.atmosphere.cpr.AtmosphereResourceImpl" /> 
    </mvc:argument-resolvers> 
</mvc:annotation-driven> 

這樣做,也導致新的錯誤:

[cvc-complex-type.2.1: Element 'mvc:annotation-driven' must have no character or element information item [children], because the type's content type is empty.] 

我也試過this。請幫我在Spring Controller中注入AtmosphereResource。我是否還需要更新web.xml或其他一些配置文件才能使其工作或缺少哪些部分。請幫忙! 也請評論其他提供服務器端事件功能的替代方案。提前致謝!

回答

1

嘗試Atmosphere 2.1.0-RC1並關注此document。你所需要做的就是將atmosphere-spring.jar添加到你的依賴項中。

- Jeanfrancois

+0

是參數解析程序是否正確? AtmosphereResourceImpl是否實現了一個Spring MethodArgumentResolver? –

2

你的論點,解析器必須是這樣的一類:

public class AtmosphereArgumentResolver implements HandlerMethodArgumentResolver { 

    @Override 
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { 
     HttpServletRequest httpServletRequest= webRequest.getNativeRequest(HttpServletRequest.class); 
     return Meteor.build(httpServletRequest).getAtmosphereResource(); 
    } 

    @Override 
    public boolean supportsParameter(MethodParameter parameter) { 
      return AtmosphereResource.class.isAssignableFrom(parameter.getParameterType()); 
    } 

} 

,並嘗試這個辦法:

<mvc:annotation-driven> 
    <mvc:argument-resolvers> 
     <bean class="com.yourpackage.AtmosphereArgumentResolver" /> 
    </mvc:argument-resolvers> 
</mvc:annotation-driven>