Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Plain View
Flume, mail # user - AvroClient connections


Copy link to this message
-
AvroClient connections
Mohit Anchlia 2012-09-06, 17:32
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();
  }
 }
}
+
Mohit Anchlia 2012-09-13, 21:34
+
Mohit Anchlia 2012-09-16, 15:36