An example of using Thrift


1 sysperfinfo.thrift  file

struct CpuUsgInfo {

  1: double usr
  2: double nice
  3: double sys
  4: double idle
  5: string msg
}

struct MemUsgInfo{
  1: i32 free
  2: i32 buffers
  3: i32 cached
  4: i32 swap
  5: string msg
}

exception ExceptionForTest {
  1: string name
  2: string description
}

service JMSPerfMonitor{
  CpuUsgInfo   getCpuUsg(),
  MemUsgInfo   getMemUsg()
}

2. Server


package cn.cnic.sdc.jobscheduler.perfmonitor.service;
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
import cn.cnic.sdc.jobscheduler.perfmonitor.datadefine.JMSPerfMonitor;

import java.util.HashMap;

public class JMSPerfMonitorServer {

  public static JMSPerfMonitorHandler handler;

  public static JMSPerfMonitor.Processor processor;

  public static void main(String [] args) {
    try {
      handler = new JMSPerfMonitorHandler();
      processor = new JMSPerfMonitor.Processor(handler);
      
      TServerTransport serverTransport = new TServerSocket(19092);
      TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
      System.out.println("Starting the JMSPerfMonitor server on 19092...");
      server.serve();
    } catch (Exception x) {
      x.printStackTrace();
    }
  }
}

 

3. Client:

package cn.cnic.sdc.jobscheduler.perfmonitor.service;
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

// Generated code
import org.apache.thrift.TException;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;

import cn.cnic.sdc.jobscheduler.perfmonitor.datadefine.CpuUsgInfo;
import cn.cnic.sdc.jobscheduler.perfmonitor.datadefine.JMSPerfMonitor;

public class JMSPerfMonitorClient {
  public static void main(String [] args) {

    try {
      TTransport transport = new TSocket("localhost", 19092);
      TProtocol protocol = new  TBinaryProtocol(transport);
      JMSPerfMonitor.Client client = new JMSPerfMonitor.Client(protocol);
      transport.open();

      CpuUsgInfo cui = client.getCpuUsg();
      if(cui != null){
          System.out.println("get a CpuusgInfo from server: ");
          //System.out.println(""+cui.getMsg());
             
            if(cui.getMsg().startsWith("Error")){
                System.out.println("msg: " + String.valueOf(cui.getMsg()));
            } else {
                System.out.println("usr: " + Double.parseDouble(String.valueOf(cui.getUsr())));
                   System.out.println("sys: " + Double.parseDouble(String.valueOf(cui.getSys())));
                   System.out.println("idle: " + Double.parseDouble(String.valueOf(cui.getIdle())));
                   System.out.println("nice: " + Double.parseDouble(String.valueOf(cui.getNice())));
            }
      }

      transport.close();
    } catch (TException x) {
      x.printStackTrace();
    }
  }

}


4. Handler


package cn.cnic.sdc.jobscheduler.perfmonitor.service;
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.thrift.TException;

import cn.cnic.sdc.jobscheduler.perfmonitor.datadefine.*;
import cn.cnic.sdc.jobscheduler.perfmonitor.monitor.CpuUsgShell;
import cn.cnic.sdc.jobscheduler.perfmonitor.monitor.MemUsgShell;


public class JMSPerfMonitorHandler implements JMSPerfMonitor.Iface {

  public JMSPerfMonitorHandler() {
    
  }

@Override
  public CpuUsgInfo getCpuUsg() {
        System.out.println("get CPU usage.");
            CpuUsgShell cpu = new CpuUsgShell();
            return cpu.getCpuUsg();
  }

@Override
public MemUsgInfo getMemUsg() throws TException {
    MemUsgShell mem = new MemUsgShell();
    return null;
}

}

5.生成命令

thrift -gen sysperfinfo.thrift


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值