titiwangsa
Jun 18th, 2007, 01:13 AM
i'm using spring 2.0.4 and the daos are simple jdbc daos
my web framework is jsf with no spring mvc or even spring web flow
i'm executing a method from a service bean in another thread
is this code safe?
public class VisitorCountSessionListener implements HttpSessionListener {
private final String BEAN_VISITOR_SERVICE = "visitorService";
public void sessionCreated(final HttpSessionEvent event) {
final Thread thread = new Thread() {
public void run() {
final VisitorService visitorService = getVisitorService(event);
visitorService.incrementVisitor();
}
};
thread.start();
}
private VisitorService getVisitorService(HttpSessionEvent event) {
final WebApplicationContext wac = getWebApplicationContext(event);
return (VisitorService) wac.getBean(BEAN_VISITOR_SERVICE);
}
private WebApplicationContext getWebApplicationContext(
HttpSessionEvent httpSessionEvent) {
final HttpSession session = httpSessionEvent.getSession();
final ServletContext sc = session.getServletContext();
return WebApplicationContextUtils.getRequiredWebApplicati onContext(sc);
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
// Do nothing
}
}
the important part is just
public void sessionCreated(final HttpSessionEvent event) {
final Thread thread = new Thread() {
public void run() {
final VisitorService visitorService = getVisitorService(event);
visitorService.incrementVisitor();
}
};
thread.start();
}
i mean is it ok to get a service bean from the application context
and use in another thread.
would there be any concurrency problems?
i'm doing this because the visitorService.incrementVisitor() function
can sometimes take a while
and the requested page cannot be started till after the visitorService.incrementVisitor() function has ended
so, i'm letting the session start and visitor count can be incremented in the background.
to put it to another level
would this code be ok?
public void sessionCreated(HttpSessionEvent event) {
final Thread thread = getVisitorThread(event);
thread.start();
}
where the thread is a prototype bean in the application context?
so
1. is starting a new thread containing a service bean (and daos inside the service bean) safe/good practice?
2. if 1 is yes, then is my posted code safe?
3. if 2 is yes, then is getting a configured, prototyped Thread bean from the application context safe/ good practice?
thanks..
my web framework is jsf with no spring mvc or even spring web flow
i'm executing a method from a service bean in another thread
is this code safe?
public class VisitorCountSessionListener implements HttpSessionListener {
private final String BEAN_VISITOR_SERVICE = "visitorService";
public void sessionCreated(final HttpSessionEvent event) {
final Thread thread = new Thread() {
public void run() {
final VisitorService visitorService = getVisitorService(event);
visitorService.incrementVisitor();
}
};
thread.start();
}
private VisitorService getVisitorService(HttpSessionEvent event) {
final WebApplicationContext wac = getWebApplicationContext(event);
return (VisitorService) wac.getBean(BEAN_VISITOR_SERVICE);
}
private WebApplicationContext getWebApplicationContext(
HttpSessionEvent httpSessionEvent) {
final HttpSession session = httpSessionEvent.getSession();
final ServletContext sc = session.getServletContext();
return WebApplicationContextUtils.getRequiredWebApplicati onContext(sc);
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
// Do nothing
}
}
the important part is just
public void sessionCreated(final HttpSessionEvent event) {
final Thread thread = new Thread() {
public void run() {
final VisitorService visitorService = getVisitorService(event);
visitorService.incrementVisitor();
}
};
thread.start();
}
i mean is it ok to get a service bean from the application context
and use in another thread.
would there be any concurrency problems?
i'm doing this because the visitorService.incrementVisitor() function
can sometimes take a while
and the requested page cannot be started till after the visitorService.incrementVisitor() function has ended
so, i'm letting the session start and visitor count can be incremented in the background.
to put it to another level
would this code be ok?
public void sessionCreated(HttpSessionEvent event) {
final Thread thread = getVisitorThread(event);
thread.start();
}
where the thread is a prototype bean in the application context?
so
1. is starting a new thread containing a service bean (and daos inside the service bean) safe/good practice?
2. if 1 is yes, then is my posted code safe?
3. if 2 is yes, then is getting a configured, prototyped Thread bean from the application context safe/ good practice?
thanks..