2013-12-17 4 views
0

Я хочу предоставить push-уведомления в проекте Spring MVC, используя JDK 1.6 для всех браузеров. Я последовал за this post и, наконец, решил пойти с Atmosphere. Для событий сервера посланных мой контроллера сервера (source):Интеграция атмосферы с весной 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. Пожалуйста, помогите мне ввести AtmosphereResource в контроллер весны. Нужно ли мне также обновлять web.xml или какой-либо другой файл конфигурации, чтобы заставить его работать или какую часть мне не хватает. Пожалуйста помоги! Просьба также прокомментировать другие варианты обеспечения функциональности серверных событий. Заранее спасибо!

ответ

1

попробуйте Atmosphere 2.1.0-RC1 и следуйте за этим document. Все, что вам нужно сделать, это добавить атмосферу-spring.jar к вашей зависимости.

- Jeanfrancois

+0

аргумент-распознаватель правильно? Does AtmosphereResourceImpl реализует весну 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> 
Смежные вопросы