用JAVA写的一个简单的协议,我们公司中间件就是采用这种协议:
如有疑问,可以和我联系: [email protected]
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.IOException;public abstract class GeProtocol
{
public abstract Object readRequest() throws IOException;
public abstract void sendRequest(Serializable msg) throws IOException;
}
import java.io.*;/**
simple transfer protocol, use serialization to
* transfer object
*/
public class GeSimpleProtocol extends GeProtocol
{
private ObjectInputStream inStream;
private ObjectOutputStream outStream;
public GeSimpleProtocol(InputStream ins, OutputStream outs)
{
try {
if (outs != null)
{
outStream = new ObjectOutputStream(new BufferedOutputStream(outs));
outStream.flush();
}
if (ins != null)
inStream = new ObjectInputStream(new BufferedInputStream(ins));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public synchronized Object readRequest() throws IOException
{
try{ return inStream.readObject() ; } catch(ClassNotFoundException e){e.printStackTrace();return null;}
}
public synchronized void sendRequest(Serializable msg) throws IOException
{
outStream.writeObject(msg);
outStream.flush();
outStream.reset();
}
}
如有疑问,可以和我联系: [email protected]
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.IOException;public abstract class GeProtocol
{
public abstract Object readRequest() throws IOException;
public abstract void sendRequest(Serializable msg) throws IOException;
}
import java.io.*;/**
simple transfer protocol, use serialization to
* transfer object
*/
public class GeSimpleProtocol extends GeProtocol
{
private ObjectInputStream inStream;
private ObjectOutputStream outStream;
public GeSimpleProtocol(InputStream ins, OutputStream outs)
{
try {
if (outs != null)
{
outStream = new ObjectOutputStream(new BufferedOutputStream(outs));
outStream.flush();
}
if (ins != null)
inStream = new ObjectInputStream(new BufferedInputStream(ins));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public synchronized Object readRequest() throws IOException
{
try{ return inStream.readObject() ; } catch(ClassNotFoundException e){e.printStackTrace();return null;}
}
public synchronized void sendRequest(Serializable msg) throws IOException
{
outStream.writeObject(msg);
outStream.flush();
outStream.reset();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货