final String zkAddr = args[0];
final String clusterName = SetupConsumerCluster.DEFAULT_CLUSTER_NAME;
final String consumerId = args[1];
final String mqServer = args[2];
ZkClient zkclient = null;
try
{
// add node to cluster if not already added
zkclient =
new ZkClient(zkAddr,
ZkClient.DEFAULT_SESSION_TIMEOUT,
ZkClient.DEFAULT_CONNECTION_TIMEOUT,
new ZNRecordSerializer());
ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
List<String> nodes = admin.getInstancesInCluster(clusterName);
if (!nodes.contains("consumer_" + consumerId))
{
InstanceConfig config = new InstanceConfig("consumer_" + consumerId);
config.setHostName("localhost");
config.setInstanceEnabled(true);
admin.addInstance(clusterName, config);
}
// start consumer
final Consumer consumer =
new Consumer(zkAddr, clusterName, "consumer_" + consumerId, mqServer);
Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
System.out.println("Shutting down consumer_" + consumerId);
consumer.disconnect();
}
});
consumer.connect();
}
finally
{
if (zkclient != null)
{
zkclient.close();
}
}
}