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