我需要一些建议来实现最好的协议(不是http,tcp,...)te发送消息。
我有一台服务器,连接了1000个客户端。服务器将任务传递给客户端。客户端在执行不同任务后发回信息。
如何使用某些参数将任务发送到JMS客户端?
任务只不过是动作和一些参数。
报告只不过是数据。
我看过很多文章,但找不到这个问题的答案。提前谢谢。
答案 0 :(得分:0)
这是我们目前的实施。
我们使用XSD定义协议,并让它生成类(POJO' s)。 这允许我们编组/解组对象并将它们作为XML对象发送。
我们的XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<!-- Ping
The server will send a ping to a client and waits for a pong.
****************************************************************** -->
<xs:element name="Ping">
<xs:complexType>
<xs:sequence>
<xs:element name="client" type="xs:string"/>
<xs:element name="message" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Pong
The client will send a pong back to the server.
****************************************************************** -->
<xs:element name="Pong">
<xs:complexType>
<xs:sequence>
<xs:element name="client" type="xs:string"/>
<xs:element name="message" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Alive
The client will send an alive message when it starts up.
The time is local client time.
****************************************************************** -->
<xs:element name="Alive">
<xs:complexType>
<xs:sequence>
<xs:element name="client" type="xs:string"/>
<xs:element name="time" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- ... Many more message definitions ...
****************************************************************** -->
</xs:schema>
我们的考试类:
public class JaxbFacadeTest {
@Test
public void testPing() throws JAXBException, SAXException, UnsupportedEncodingException {
System.out.println("testPing");
Ping ping = new Ping();
ping.setClient("guid-client");
ping.setMessage("Ping Message");
String marshalToString = JaxbFacade.getInstance().marshalToString(ping);
System.out.println(marshalToString);
}
@Test
public void testPong() throws JAXBException, SAXException, UnsupportedEncodingException {
System.out.println("testPong");
Pong pong = new Pong();
pong.setClient("guid-client");
pong.setMessage("Ping Message");
String marshalToString = JaxbFacade.getInstance().marshalToString(pong);
System.out.println(marshalToString);
}
@Test
public void testAlive() throws JAXBException, SAXException, UnsupportedEncodingException {
System.out.println("testAlive");
Date now = new Date();
Alive alive = new Alive();
alive.setClient("guid-client");
alive.setTime(now.toString());
String marshalToString = JaxbFacade.getInstance().marshalToString(alive);
System.out.println(marshalToString);
}
//Many more
}
使用maven生成类:
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/xsd</directory>
<targetPath>com/test/package/client/jaxb</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<id>jaxb</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generatePackage>com.test.package.client.jaxb</generatePackage>
<schemaDirectory>${project.basedir}/src/main/xsd</schemaDirectory>
</configuration>
</plugin>
</plugins>
</build>