I have a below class that I use for AvroClient. My question is it ok to
share just one connection with many threads or should one have one instance
of this class per thread?
public class AvroClient {
private static String hostName = "dslg1";
private static String localHostName = "unknown";
private static int port = 41414;
private RpcClient rpcClient = null;
public AvroClient(String hostName, int port) {
this.hostName = hostName;
this.port = port;
}
public AvroClient() { initLocalHostName();}
public AvroClient(String host) {
this.hostName = host;
initLocalHostName();
}
private void initLocalHostName() {
localHostName = NetworkInfo.getLocalHostName();
}
public void sendDataToFlume(String data) {
// Create flume event object
Event event = EventBuilder.withBody(data, Charset.forName("UTF-8"));
Map<String,String> headers = new HashMap<String,String>();
headers.put("host", localHostName);
event.setHeaders(headers);
try {
rpcClient.append(event);
} catch (EventDeliveryException e) {
connect();
}
}
public void connect() {
if (null != rpcClient) {
rpcClient.close();
rpcClient = null;
}
rpcClient = RpcClientFactory.getDefaultInstance(hostName, port);
}
public void disconnect() {
// close the rpc connection
if (null != rpcClient) {
rpcClient.close();
}
}
}