aspose file tools*
The moose likes Other Application Frameworks and the fly likes Thread.sleep(n) problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Thread.sleep(n) problem" Watch "Thread.sleep(n) problem" New topic
Author

Thread.sleep(n) problem

gangadhar katakam
Greenhorn

Joined: Oct 26, 2010
Posts: 2
Hi,

Congratulations on releasing the Camel In Action book.

I have an issue with Thread.sleep(n) on context thread.
I have created my route with a switch and added the route to the context.
I have started the context using context.start() method
then i am making context thread to sleep for some time. [Thread.sleep(4000) - context will take time to initialize the routes]
But this thread is finishing in less than 4 Sec. So, i am waiting for the thread to awake.
Here i have couple of points.
1) Route establishment and initialization can take time longer than the time which we are setting in thread.sleep().
2) Route establishment and initialization can take less time than the thread.sleep()
is there any other way to make context thread not to sleep after finishing its task?

Thanks in advance.
Claus Ibsen
author
Greenhorn

Joined: Apr 01, 2004
Posts: 18
What context are you talking about?

CamelContext or some Spring stuff?

if you invoke start() on CamelContext then it will start up, initialize all routes, start all routes and then thereafter return from the start() method.

Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
gangadhar katakam
Greenhorn

Joined: Oct 26, 2010
Posts: 2
Thank you Claus for your immediate reply.

Here i am describing the problem:

I am using camel-snmp component to communicate with the SNMP supported switches. Here is an example which we are using:

final CamelContext context = new DefaultCamelContext();
context.addRoutes(
new RouteBuilder() {

@Override
public void configure() throws Exception {
// TODO Auto-generated method stub
from(snmpSwitchURI)
.process(new Processor() {

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void process(final Exchange exchange)
throws Exception {
// TODO Auto-generated method stub
final Object object = (Object) exchange.getIn()
.getBody();
if (object instanceof List) {
final List<TableEvent> tableEvents = (List) object;
// QE & Dev verification raw data which we are
// getting from Camel
LOGGER.debug("Performance metrics raw data:\n"
+ tableEvents);
if (!tableEvents.isEmpty()) {
final Map perfMetrics = processTableEvents(
object, ipAddress);
exchange.getIn().setBody(
convertToSRMFormat(dcfAttributes,
perfMetrics, ipAddress));
}
}
}
}).marshal().json(JsonLibrary.Jackson)
.convertBodyTo(String.class)
.process(new Processor() {

@Override
public void process(final Exchange exchange)
throws Exception {
srmJsonData.add(exchange.getIn().getBody());
}
});
}
}
);
context.start();
Thread.sleep(4000); // We are setting the thread to sleep for 4sec.
context.stop();

Here we have extended camel to support SNMP4j TableUtils using SNMP V3. Here we will get TableEvents from the SNMP4J framework. We are establishing the route and to receive the TableEvents is taking some times more than 4 Sec or less than 4 Sec. If it takes more than 4 Sec we are not getting the TableEvents. Hence we thought of making it dynamic like after finishing the context routing process, we want to get the TableEvent instead of setting Thread.sleep(). Please provide how can i overcome this issue.


Claus Ibsen
author
Greenhorn

Joined: Apr 01, 2004
Posts: 18
The Camel mailing list is where you should take technical issues.
http://camel.apache.org/discussion-forums.html

This is more general about the book and Camel in general.

Anyway the book talks about running Camel in chapter 13. And it shows how you can start up Camel, let a route run for some time, or let the route be able to stop when it has finished processed X messages.
And chapter 12 talks about an event notifier which allows you to have notifications when certain events occur.

Such as message completed, failed, etc.

So all together you got some features to help you when to call stop on CamelContext.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread.sleep(n) problem