【发布时间】:2014-05-15 07:56:55
【问题描述】:
我正在尝试使用 Jersey SSE 官方文档中给出的示例示例
请参阅以下链接中的“14.5.2. 使用 EventSource 进行异步 SSE 处理” https://jersey.github.io/documentation/2.8/user-guide.html#example-simple-sse
我的代码如下
客户端代码-
public class ClientSSEEventManager {
public void WaitForEvents() {
// Client client = ClientBuilder.newBuilder()
// .register(SseFeature.class).build();
// WebTarget target =
// client.target("http://localhost:8080/server/events");
//
// EventInput eventInput = target.request().get(EventInput.class);
// while (!eventInput.isClosed()) {
// final InboundEvent inboundEvent = eventInput.read();
// if (inboundEvent == null) {
// // connection has been closed
// break;
// }
// System.out.println(inboundEvent.getName() + "; "
// + inboundEvent.readData(String.class));
// }
Client client = ClientBuilder.newBuilder().register(SseFeature.class)
.build();
WebTarget target = client.target("http://localhost:8080/server/events");
EventSource eventSource = EventSource.target(target).build();
EventListener listener = new EventListener() {
@Override
public void onEvent(InboundEvent inboundEvent) {
System.out.println(inboundEvent.getName() + "; "
+ inboundEvent.readData(String.class));
}
};
eventSource.register(listener, "message-to-client");
eventSource.open();
}
}
public class MyApplication extends ResourceConfig {
public MyApplication(){
super(ClientSSEEventManager.class, SseFeature.class);
}
// Set<Class<?>> classes = new HashSet<Class<?>>() {
// /**
// *
// */
// private static final long serialVersionUID = 1L;
//
// { add(ClientSSEEventManager.class);
// }};
//
// @Override
// public Set<Class<?>> getClasses() {
// return classes;
// }
}
然后在其中一种操作方法中,我只是初始化事件监听如下
//Start listening to event from server
ClientSSEEventManager clientSSEEventManager = new ClientSSEEventManager();
clientSSEEventManager.WaitForEvents();
///
客户端的 Web.xml 有如下的 init-param
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.framework.MyApplication</param-value>
</init-param>
服务器代码 -
@Path("events")
public class ServerSSEServerEventManager {
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getNotificationEvents(){
final EventOutput eventOutput = new EventOutput();
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
// ... code that waits 1 second
final OutboundEvent.Builder eventBuilder
= new OutboundEvent.Builder();
eventBuilder.name("message-to-client");
eventBuilder.data(String.class,
"Hello world " + i + "!");
final OutboundEvent event = eventBuilder.build();
eventOutput.write(event);
}
} catch (IOException e) {
throw new RuntimeException(
"Error when writing the event.", e);
} finally {
try {
eventOutput.close();
} catch (IOException ioClose) {
throw new RuntimeException(
"Error when closing the event output.", ioClose);
}
}
}
}).start();
return eventOutput;
}
}
客户端的预期输出如下
message-to-client; Hello world 0!
message-to-client; Hello world 1!
message-to-client; Hello world 2!
message-to-client; Hello world 3!
message-to-client; Hello world 4!
message-to-client; Hello world 5!
message-to-client; Hello world 6!
message-to-client; Hello world 7!
message-to-client; Hello world 8!
message-to-client; Hello world 9!
但客户端没有打印任何内容。 我在这里错过了什么吗? 我有疑问, client.Target 应该有 "http://:8080/server/events" 吗?或者它应该只是“http://:8080/events”
【问题讨论】:
-
您的服务器 URL 取决于您的应用程序的
Context Path。如果您部署为server,那么您的路径应该是http://localhost:8080/server/events。我会在您的服务器端进行一些登录,并将 Jackson 日志过滤器添加到您的客户端client.register(new LoggingFilter(LOG, true));,这样您就可以看到您的客户端正在发送/接收什么。 -
如果我直接在浏览器中打开 URL,例如 http://<8080>8080>
标签: java web-services server-sent-events jersey-2.0