Hi all
Iam trying to write a web application which is invoked when the message arrives
in topic. The code is waiting for a message in kafka consumer in while loop and
sometimes it picks up the message and sometimes its waiting forever even when
the message is produced in the topic. I am invoking the kafka consumer in
servlet init method as a result its waiting in the while and not fully
deploying the web app. I would appreciate any suggestions as iam looking to
invoke my consumer as soon any message arrives in topic.
Map<String, Integer> topicMap = new HashMap<String, Integer>();
// 1 represents the single thread
topicMap.put(topic, new Integer(1));
Map<String, List<KafkaStream<byte[], byte[]>>>
consumerStreamsMap = consumerConnector
.createMessageStreams(topicMap);
// Get the list of message streams for each topic,
using the default
// decoder.
KafkaStream<byte[], byte[]> stream =
consumerStreamsMap.get(topic).get(0);
ConsumerIterator<byte[], byte[]> it =
stream.iterator();
try {
// for (final KafkaStream<byte[],
byte[]> stream : streamList) {
// ConsumerIterator<byte[], byte[]>
consumerIte = stream.iterator();
// reads from Kafka until you stop it.
while (it.hasNext()) {
//
System.out.println("Message from Single Topic :: " +
// new
byte[] msg =
((MessageAndMetadata<byte[], byte[]>) it.next()).message();
// byte[] rawMsg =
msg.message();
// byte[] ObjInBytes =
it.next().message();
final RequestMessage
msgAsObject = convertFromBytes(msg);
if (msgAsObject != null)
{
Thread
thread = new Thread() {
public void run() {
logger.info("Starting new Thread for request Object" +
msgAsObject.getRequestId());
StateMachine sm = new StateMachine(msgAsObject);
sm.processPdfaRequest();
System.out.println("Request Fully completed");
logger.info("Request Fully completed");
Thread.currentThread().stop();
sm = null;
}
};
thread.run();
}
}
} catch (Exception e) {
e.printStackTrace();
}
Regards
Surender Kudumula
Big Data Consultant - EMEA
Analytics & Data Management
[email protected]<mailto:[email protected]>
M +44 7795970923
Hewlett-Packard Enterprise
Cain Rd,
Bracknell
RG12 1HN
UK
[http://graphics8.nytimes.com/images/2015/06/03/technology/03bits-hp/03bits-hp-master315.png]