我正在使用@Subscribe
和@Publish
注释开发RESTful pub子应用程序。
服务器崩溃或网络故障后,wAsync会触发Event.CLOSE
事件,但无法重新连接大气服务器。频道已创建,但现有的客户端未受到影响。然而,新客户可以接受该渠道。
请注意,我没有任何日志。
软件堆栈
我的 web.xml
<servlet>
<description>AtmosphereServlet</description>
<servlet-name>AtmosphereServlet</servlet-name>
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
com.mesview.rest
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.websocket.messageContentType</param-name>
<param-value>application/json</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.AtmosphereInterceptor</param-name>
<param-value>org.atmosphere.interceptor.HeartbeatInterceptor</param-value>
</init-param>
<async-supported>true</async-supported>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
泽西岛实施
@Path("metallica")
public class MetallicaService {
@GET
@Path("/suscripcion")
@Subscribe(value = "metallica", timeout = -1)
public String handshake() {
return null;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Track getTrackInJSON() {
Track track = new Track();
track.setTitle("Enter Sandman");
track.setSinger("Metallica");
return track;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Publish("metallica")
public String createTrackInJSON(Track track) {
String result = "Metallica -- Track saved : " + track;
//return Response.status(201).entity(result).build();
return result;
}
}
最后,客户
public class MetallicaClient {
public static void main(String[] args) {
final Client client = ClientFactory.getDefault().newClient();
final RequestBuilder request = client.newRequestBuilder()
.method(Request.METHOD.GET)
.uri("http://localhost:8080/metallica/suscripcion")
.encoder(new Encoder<String, Reader>() { // Stream the request body
@Override
public Reader encode(String s) {
System.out.println("Encode: " + s);
return new StringReader(s);
}
})
.decoder(new Decoder<String, Reader>() {
@Override
public Reader decode(Event type, String s) {
if (type.equals(Event.MESSAGE)) {
System.out.println("---- Message received ----");
System.out.println("---- Date and time: " + Calendar.getInstance().getTime().toString() + " ----");
}
System.out.println("Decode: " + s);
return new StringReader(s);
}
})
.transport(Request.TRANSPORT.WEBSOCKET) // Try WebSocket
.transport(Request.TRANSPORT.LONG_POLLING); // Fallback to Long-Polling
final OptionsBuilder<DefaultOptions, DefaultOptionsBuilder> clientOptions = client.newOptionsBuilder()
.reconnect(true)
.pauseBeforeReconnectInSeconds(60);
final Socket socket = client.create(clientOptions.build());
try {
socket.on("message", new Function<String>() {
@Override
public void on(String s) {
System.out.println("Respuesta: " + s);
}
}).on(new Function<IOException>() {
@Override
public void on(IOException e) {
e.printStackTrace();
}
}).on(Event.CLOSE.name(), new Function<String>() {
@Override
public void on(String s) {
System.out.println("Se ha perdido la conexión con el servidor.");
try {
socket.open(request.build(), 60, TimeUnit.SECONDS);
} catch (IOException e) {
e.printStackTrace();
}
}
}).on(Event.ERROR, new Function<String>() {
@Override
public void on(String s) {
System.out.println("Error: " + s);
System.out.println("Se ha perdido la conexión con el servidor.");
try {
socket.open(request.build(), 60, TimeUnit.SECONDS);
} catch (IOException e) {
e.printStackTrace();
}
}
}).on(Event.REOPENED.name(), new Function<String>() {
@Override
public void on(String s) {
System.out.println("Reconexión.");
}
}).open(request.build());
} catch (IOException e) {
e.printStackTrace();
}
}
}
处有个回购案例