Professional Documents
Culture Documents
Apache Thrift: Thrift - Exe - R - Gen Java Hello - Thrift
Apache Thrift: Thrift - Exe - R - Gen Java Hello - Thrift
Thrift has a small IDL (interface definition language) and comes with an IDL compiler
that generates code to be used to easily build RPC clients and servers that
communicate seamlessly across programming languages.
Since the thrift is distributed in source form so we first need to build the thrift.exe
IDL compiler. Follow this document to generate the compiler exe on windows.
Following is an example usage. We intend to write a C# service and a Java client that
implement the following interface (hello.thrift):
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
}
struct Work {
1: i32 num1 = 0,
2: i32 num2,
3: Operation op,
4: string comment,
}
exception InvalidOperation {
1: i32 what,
2: string why
}
service HelloService {
void ping(),
string greeting(),
Compile the above thrift file with the thrift compiler to generate both java and c#
code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thrift;
using Thrift.Transport;
using Thrift.Protocol;
using Thrift.Server;
using hello;
namespace thriftcsserv
{
class Program
{
static void Main(string[] args)
{
try
{
HelloServiceHandler handler = new
HelloServiceHandler();
HelloService.Processor processor = new
HelloService.Processor(handler);
TServerTransport transport = new TServerSocket(34568);
TServer server = new TSimpleServer(processor,
transport);
package hello.client;
import hello.HelloService;
import hello.InvalidOperation;
import hello.Operation;
import hello.Work;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
transport.open();
client.ping();
System.out.println("ping()");
work.op = Operation.DIVIDE;
work.num1 = 1;
work.num2 = 0;
try {
int quotient = client.calculate(1, work);
System.out.println("Whoa we can divide by 0");
} catch (InvalidOperation io) {
System.out.println("Invalid operation: " + io.why);
}
work.op = Operation.SUBTRACT;
work.num1 = 15;
work.num2 = 10;
try {
int diff = client.calculate(1, work);
System.out.println("15-10=" + diff);
} catch (InvalidOperation io) {
System.out.println("Invalid operation: " + io.why);
}
transport.close();
} catch (TException x) {
x.printStackTrace();
}
}
}
Now run the service and then the client. The o/p is as follows:
ping()
Hello world of RPC using thrift from c# service
1+1=2
Invalid operation: Cannot divide by 0
15-10=5
What we learned?
We can exchange objects and throw exceptions and invoke methods and all of it can
be done using TCP stream socket interface.
We can use the above model to implement C# SDK Connector which can be a windows
RPC service and the SCOM plug-in on DCFM server will be the RPC client. The methods
that we will need are:
1. Push fabrics info (name: url) data to SCOM to dynamically create nodes.
2. Log critical SAN fabric events.
/*
* Fabric name and url pair. Will be passed from SCOM
* plug-in client to create a UrlViewType node in the
* monitoring tab of SCOM Console, dynamically.
*/
struct Fabric {
1: string name ,
2: string url,
}
/*
* A critical SAN event that is logged into windows
* event log.
*/
struct SANEvent {
1: string sourceName ,
2: string eventId,
3: string severity,
4: string description,
}
enum ErrorCode {
CONNECTION_FAILURE = 1,
AUTH_FAILURE = 2,
}
/*
* SCOM Exception.
*/
exception SCOMConnectorException {
1: i32 what,
2: string why
}
/*
* Service interface.
*/
service SCOMConnectorService {
/*
* Will create or update fabric nodes in monitoring tab
* of SCOM Console. It will be called periodically with
* the then current set of fabric names and url pairs.
*/
void createOrUpdateNodes(list<Fabric> fabrics) throws
(1:SCOMConnectorException err),
/*
* Log SAN critical events to windows event log.
*/
void logSANEvent(SANEvent event) throws (1:SCOMConnectorException err),
}