Java 在客户端使用CometD获取传输和EOF异常
在使用Comet时,我在客户端收到传输异常和EOF异常。我试图通过Comet文档找到一个解决方案,我也在谷歌上搜索,但没有找到任何解决方案 下面是我得到的异常的堆栈跟踪Java 在客户端使用CometD获取传输和EOF异常,java,javascript,cometd,Java,Javascript,Cometd,在使用Comet时,我在客户端收到传输异常和EOF异常。我试图通过Comet文档找到一个解决方案,我也在谷歌上搜索,但没有找到任何解决方案 下面是我得到的异常的堆栈跟踪 CommetD Connected!!!!!!! cometDURL http://192.168.210.106:8080/cometd2/cometd subscriberChannel /service/java In finally........... 2013-09-12 10:20:26,200 [HttpClie
CommetD Connected!!!!!!!
cometDURL http://192.168.210.106:8080/cometd2/cometd
subscriberChannel /service/java
In finally...........
2013-09-12 10:20:26,200 [HttpClient-201] INFO org.cometd.client.BayeuxClient.180910783 - Messages failed [{id=31, connectionType=long-polling, channel=/meta/connect, clientId=2qpitkhfcjh1g511al4xal9vltg}]
org.cometd.common.TransportException at org.cometd.client.transport.LongPollingTransport$TransportExchange.onResponseComplete(LongPollingTransport.java:334) at org.eclipse.jetty.client.HttpExchange$Listener.onResponseComplete(HttpExchange.java:999)
at org.eclipse.jetty.client.HttpExchange.setStatus(HttpExchange.java:288)
at org.eclipse.jetty.client.HttpConnection$Handler.messageComplete(HttpConnection.java:628)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:786)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:220)
at org.eclipse.jetty.client.HttpConnection.handle(HttpConnection.java:275)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:545)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:43)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:529)
at java.lang.Thread.run(Thread.java:662)
java.io.EOFException: closed: TransportExchange@50115a3f=POST//192.168.145.210:8080/cometd2/cometd#WAITING(64ms)sent=64ms
at org.eclipse.jetty.client.HttpConnection.close(HttpConnection.java:663)
at org.eclipse.jetty.client.HttpDestination.close(HttpDestination.java:639)
at org.eclipse.jetty.client.HttpClient.doStop(HttpClient.java:491)
at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:83)
at com.commetd.CommetD.publish(CommetD.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:301)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.GeneratedMethodAccessor72.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:301)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy25.callProxy(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:301)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy171.publishEventToBulletinManager(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
我使用以下代码连接到CometD服务器
public static boolean publish(String cometDURL,String subscriberChannel,Map<String,Object> msgToPublish) throws Exception{
boolean isSuccess = true;
HttpClient httpClient = new HttpClient();
BayeuxClient client = null;
ClientTransport transport=null;
// Here setup Jetty's HttpClient, for example:
httpClient.setMaxConnectionsPerAddress(10);
try {
httpClient.start();
Map<String, Object> options = new HashMap<String, Object>();
transport = LongPollingTransport.create(options, httpClient);
client = new BayeuxClient(cometDURL, transport);
client.handshake();
boolean handshaken = client.waitFor(1000, BayeuxClient.State.CONNECTED);
if(handshaken){
System.out.println("CommetD Connected!!!!!!!");
/*Map<String,Object> data = new HashMap<String,Object>();
data.put("name", "Java msg......... Dynamic msg.....");*/
// Fill in the data
client.getChannel(subscriberChannel).publish(msgToPublish);
}else{
isSuccess=false;
System.out.println("CommetD not connected:-(");
}
} catch (Exception e) {
isSuccess=false;
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
client.disconnect();
client.waitFor(1000, BayeuxClient.State.DISCONNECTED);
transport.terminate();
httpClient.stop();
}
return isSuccess;
}
公共静态布尔发布(字符串cometDURL、字符串subscriberChannel、映射msgToPublish)引发异常{
布尔值isSuccess=true;
HttpClient HttpClient=新HttpClient();
BayeuxClient=null;
ClientTransport=null;
//此处设置Jetty的HttpClient,例如:
httpClient.setMaxConnectionsPerAddress(10);
试一试{
httpClient.start();
Map options=newhashmap();
transport=LongPollingTransport.create(选项,httpClient);
客户=新BayeuxClient(cometDURL,运输);
client.handshake();
布尔握手=client.waitFor(1000,BayeuxClient.State.CONNECTED);
如果(握手){
System.out.println(“CommetD Connected!!!!!!!”;
/*映射数据=新的HashMap();
data.put(“名称”,“Java消息…”动态消息…”)*/
//填写数据
client.getChannel(subscriberChannel).publish(msgToPublish);
}否则{
isSuccess=false;
System.out.println(“未连接的CommetD:-(”);
}
}捕获(例外e){
isSuccess=false;
//TODO自动生成的捕捉块
e、 printStackTrace();
}最后{
client.disconnect();
client.waitFor(1000,BayeuxClient.State.DISCONNECTED);
transport.terminate();
httpClient.stop();
}
返回成功;
}
服务器端代码为
@Listener("/service/java")
public void processMsgFromJava(ServerSession remote, ServerMessage.Mutable message)
{
System.out.println("Received msg from java..................");
Map<String, Object> input = message.getDataAsMap();
String eventId = (String)input.get("eventID");
String updatedDate = (String)input.get("updatedDate");
// String channelName =(String)input.get("ChannelToPublish");
System.out.println("msg received : "+eventId+":"+updatedDate);
// Map<String, Object> output = new HashMap<String, Object>();
//output.put("greeting", name);
// remote.deliver(serverSession, "/java/test", output, null);
String channelName = "/java/test";
// Initialize the channel, making it persistent and lazy
bayeux.createIfAbsent(channelName, new ConfigurableServerChannel.Initializer()
{
public void configureChannel(ConfigurableServerChannel channel)
{
channel.setPersistent(true);
channel.setLazy(true);
}
});
// System.out.println("HHHHHHHHHHHHREEEEEEEEEEEEEEEEEEEEEEEEERRRRRRRRRRRRRRRRRRRRRRRRRRRRREEEEEEEEEE");
// Convert the Update business object to a CometD-friendly format
/* Map<String, Object> data = new HashMap<String, Object>();
data.put("javamsg", name);*/
// Publish to all subscribers
ServerChannel channel = bayeux.getChannel(channelName);
channel.publish(serverSession, input, null);
}
@Listener(“/service/java”)
public void processMsgFromJava(ServerSession远程、ServerMessage.Mutable消息)
{
System.out.println(“从java收到消息”);
映射输入=message.getDataAsMap();
String eventId=(String)input.get(“eventId”);
String UpdateDate=(String)input.get(“UpdateDate”);
//字符串channelName=(字符串)input.get(“ChannelToPublish”);
System.out.println(“收到的消息:+eventId+”:“+updateDate”);
//映射输出=新的HashMap();
//输出.put(“问候语”,名称);
//deliver(serverSession,“/java/test”,输出,null);
字符串channelName=“/java/test”;
//初始化通道,使其持久化和惰性
createIfAbsent(channelName,新的ConfigurableServerChannel.Initializer()
{
公用void配置通道(ConfigurableServerChannel)
{
channel.setPersistent(true);
channel.setLazy(true);
}
});
//System.out.println(“hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh;
//将更新业务对象转换为Comet友好格式
/*映射数据=新的HashMap();
data.put(“javamsg”,名称)*/
//向所有订阅者发布
ServerChannel=bayeux.getChannel(channelName);
发布(serverSession,input,null);
}
客户端的javascript:
(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('/java/test', function(message)
{
$('#body').append('<div>Server Says: ' + message.data.eventID + ':'+ message.data.updatedDate + '</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);
(函数($)
{
var cometd=$.cometd;
$(文档).ready(函数()
{
函数_连接已建立()
{
$('#body').append('已建立Comed连接');
}
函数_connectionbreaked()
{
$('#body').append('CometD Connection breaked');
}
函数_connectionClosed()
{
$('#body').append('CometD Connection Closed');
}
//管理与Bayeux服务器的连接状态的函数
var _connected=false;
功能_元连接(消息)
{
if(cometd.isDisconnected())
{
_连接=错误;
_连接关闭();
返回;
}
var wasconned=_connected;
_已连接=消息。成功===真;
如果(!wasconned&&u connected)
{
_已建立连接();
}
否则如果(已连接&&!\u已连接)
{
_连接断开();
}
}
//第一次联系服务器时调用的函数
//当服务器失去此客户端的状态时
功能_元握手(握手)
{
if(handshake.successful==true)
{
cometd.batch(函数()
{
cometd.subscribe('/java/test',函数(消息)
{
$('#body').append('服务器说:'+message.data.eventID+':'+message.data.updateDate+'');
});
//在服务通道上发布,因为消息仅用于服务器
//publish('/service/hello',{name:'World'});
});
}
}
//卸载页面时断开连接
$(窗口)。卸载(函数()
{
cometd.disconnect(真);
});
var cometURL=location.protocol+“/”+location.host+config.contextPath+“/cometd”;
cometd.configure({
url:cometURL,
日志级别:“调试”
});
cometd.addListener('/meta/handshake',_metaHandshake);
addListener('/meta/connect',_metaConnect);
cometd.握手();
});
})(jQuery);
有人能指导我解决这个问题吗?我不知道我哪里出了问题。首先,您使用的是名为
com.commetd.commetd
的代码,它不是标准代码。不清楚您显示的代码是否是这个类的代码
假设它是原始Comed代码的一个简单包装器,堆栈跟踪解释了这一切:您正在从publish()
方法中停止HttpClient
,这毫无意义
请仔细阅读CometD以及如何使用CometD。Hi sbordet,感谢您的回复。我在这里共享的发布方法是我编写的连接CometD和t的方法