lundi 13 avril 2015

Cometd giving multiple enpoints may not be deployed to same path [/cometd]

I am working on a Spring-MVC application in which I am trying to integrate Chat functionality with Cometd. I am getting the error that multiple enpoints cannot be deployed in the same path. Can anyone tell me what I am doing wrong and how to resolve it. Thanks a lot.


Error log :



org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cometDInitializer.Processor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.cometd.bayeux.server.BayeuxServer com.journaldev.spring.chat.CometDInitializer$Processor.bayeuxServer; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bayeuxServer' defined in class path resource [com/journaldev/spring/chat/CometDInitializer.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/cometd]
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)


If someone wants the complete error log, Here it is on pastebin


CometdInitializer.java:



@Component
public class CometDInitializer implements ServletContextAware
{
private ServletContext servletContext;

@Bean(initMethod = "start", destroyMethod = "stop")
public BayeuxServer bayeuxServer()
{
BayeuxServerImpl bean = new BayeuxServerImpl();
bean.setTransports(new WebSocketTransport(bean), new JSONTransport(bean), new JSONPTransport(bean));
servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bean);
bean.setOption(ServletContext.class.getName(), servletContext);
bean.setOption("ws.cometdURLMapping", "/cometd/*");
return bean;
}

public void setServletContext(ServletContext servletContext)
{
this.servletContext = servletContext;
}

@Component
public static class Processor implements DestructionAwareBeanPostProcessor
{
@Inject
private BayeuxServer bayeuxServer;
private ServerAnnotationProcessor processor;

@PostConstruct
private void init()
{
this.processor = new ServerAnnotationProcessor(bayeuxServer);
}

@PreDestroy
private void destroy()
{
}

public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
{
processor.processDependencies(bean);
processor.processConfigurations(bean);
processor.processCallbacks(bean);
return bean;
}

public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
{
return bean;
}

public void postProcessBeforeDestruction(Object bean, String name) throws BeansException
{
processor.deprocessCallbacks(bean);
}
}
}


HelloService.java :



@Named
@Singleton
@Service("helloService")
public class HelloService
{
@Inject
private BayeuxServer bayeux;
@Session
private ServerSession serverSession;

@PostConstruct
public void init()
{
}

@Listener("/service/hello")
public void processHello(ServerSession remote, ServerMessage message)
{
System.out.println("We recieved a helo msg");
Map<String, Object> input = message.getDataAsMap();
String name = (String)input.get("name");

Map<String, Object> output = new HashMap<>();
output.put("greeting", "Hello, " + name);
remote.deliver(serverSession, "/hello", output);
}
}


Application.js :



(function($)
{
var cometd = $.cometd;

$(document).ready(function()
{
function _connectionEstablished()
{
$('#body').append('<div>CometD Connection Established</div>');
}

function _connectionBroken()
{
$('#body').append('<div>CometD Connection Broken</div>');
}

function _connectionClosed()
{
$('#body').append('<div>CometD Connection Closed</div>');
}

// Function that manages the connection status with the Bayeux server
var _connected = false;
function _metaConnect(message)
{
if (cometd.isDisconnected())
{
_connected = false;
_connectionClosed();
return;
}

var wasConnected = _connected;
_connected = message.successful === true;
if (!wasConnected && _connected)
{
_connectionEstablished();
}
else if (wasConnected && !_connected)
{
_connectionBroken();
}
}

// Function invoked when first contacting the server and
// when the server has lost the state of this client
function _metaHandshake(handshake)
{
if (handshake.successful === true)
{
cometd.batch(function()
{
cometd.subscribe('/hello', function(message)
{
$('#body').append('<div>Server Says: ' + message.data.greeting + '</div>');
});
// Publish on a service channel since the message is for the server only
cometd.publish('/service/hello', { name: 'World' });
});
}
}

// Disconnect when the page unloads
$(window).unload(function()
{
cometd.disconnect(true);
});

var cometURL = location.protocol + "//" + location.host + config.contextPath + "/cometd";
cometd.configure({
url: cometURL,
logLevel: 'debug'
});

cometd.addListener('/meta/handshake', _metaHandshake);
cometd.addListener('/meta/connect', _metaConnect);

cometd.handshake();
});
})(jQuery);


Any help would be nice. Thanks a lot. :-)


Aucun commentaire:

Enregistrer un commentaire