apache java windows_Apache Avro实例windows java版本--实现内网Tcp协议数据传输

找了很久没找着java的avro例子,下面有个例子,可作为avro的入门学习

其消息模型是用Json写的,使用的时候把Main方法改成供调用的方法,并传入参数,

在cmd给main传参可以如下执行:java ** 8080。--**为编译后的java类名,8080为

传入的参数,多个参数用空格隔开,不过在cmd下容易出错,建议在eclipse中执行;

在eclipse中,对带main的类点击右键-》Run Configrations-》左侧选择要运行的程序,

右侧找到(X)=Arguments,在Program

Arguments里头输入参数,如8080-》apply-》run。

package avro;

import org.apache.avro.Protocol;

import org.apache.avro.Protocol.Message;

import org.apache.avro.generic.GenericRecord;

import org.apache.avro.ipc.HttpServer;

import org.apache.avro.ipc.generic.GenericResponder;

public class Server extends GenericResponder {

private Protocol protocol = null;

private int port;

public Server(Protocol protocol, int port)

{

super(protocol);

this.protocol = protocol;

this.port = port;

}

public Object respond(Message message, Object

request) throws Exception {

GenericRecord req =

(GenericRecord) request;

GenericRecord msg =

(GenericRecord) (req.get("message"));

// process the request

// …

return msg;

}

public void run() {

try {

HttpServer

server = new HttpServer(this, port);

server.start();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

if (args.length != 1)

{ //表示传入到args[]数组的参数长度为1

System.out.println("Usage:

Server port");

System.exit(0);

}

int port =

Integer.parseInt(args[0]); //表示获取数字第一个元素

new Server(Utils.getProtocol(),

port).run();

}

}

Client代码

package avro;

import java.net.URL;

import org.apache.avro.Protocol;

import org.apache.avro.generic.GenericData;

import org.apache.avro.generic.GenericRecord;

import org.apache.avro.ipc.HttpTransceiver;

import org.apache.avro.ipc.Transceiver;

import org.apache.avro.ipc.generic.GenericRequestor;

public class Client {

private Protocol protocol = null;

private String host = null;

private int port = 0;

private int size = 0;

private int count = 0;

public Client(Protocol protocol, String host,

int port, int size, int count) {

this.protocol = protocol;

this.host = host;

this.port = port;

this.size = size;

this.count = count;

}

public long sendMessage() throws Exception

{

GenericRecord requestData = new

GenericData.Record(

protocol.getType("message"));

// initiate the request

data

// …

GenericRecord request = new

GenericData.Record(protocol.getMessages()

.get("sendMessage").getRequest());

request.put("message",

requestData);

Transceiver t = new

HttpTransceiver(new URL("http://" + host + ":"

+

port));

GenericRequestor requestor =

new GenericRequestor(protocol, t);

long start =

System.currentTimeMillis();

for (int i = 0; i

< count; i++) {

requestor.request("sendMessage",

request);

}

long end =

System.currentTimeMillis();

System.out.println(end -

start);

return end - start;

}

public long run() {

long res = 0;

try {

res =

sendMessage();

} catch (Exception e) {

e.printStackTrace();

}

return res;

}

public static void main(String[] args) throws

Exception {

if (args.length != 4)

{ //需要传入4个参数

System.out.println("Usage:

Client host port dataSize count");

System.exit(0);

}

String host = args[0];

int port =

Integer.parseInt(args[1]);

int size =

Integer.parseInt(args[2]);

int count =

Integer.parseInt(args[3]);

new Client(Utils.getProtocol(),

host, port, size, count).run();

}

}

Utils 代码--从消息模式中获取数据

package avro;

import java.io.File;

import java.io.IOException;

import java.net.URL;

import org.apache.avro.Protocol;

public class Utils {

public

static Protocol getProtocol() {

Protocol protocol = null;

try {

URL url =

Utils.class.getClassLoader().getResource("message.avpr");

protocol = Protocol.parse(new File(url.getPath()));

} catch (IOException e) {

e.printStackTrace();

}

return protocol;

}

}

message.avpr

{

"namespace":

"avro",

"protocol":

"messageProtocol",

"doc": "This

is a message.",

"name":

"Message",

"types":

[

{"name":"message", "type":"record",

"fields":[

{"name":"name", "type":"string"},

{"name":"type", "type":"int"},

{"name":"price", "type":"double"},

{"name":"valid", "type":"boolean"},

{"name":"content", "type":"bytes"}

]}

],

"messages": {

"sendMessage":{

"doc" : "test",

"request" :[{"name":"message","type":"message" }],

"response" :"message"

} } }

需要avro的jar包跟json的两个jar包,调用的时候把main改成方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值