Reputation: 9011
So I have somewhat limited experience with serialization, Wicket, and multi thread projects so bear with me.
Essentially my web application class is instantiating a POJ (parentObject) which creates a starts a new timer and instantiates several childObjects that also have timers in them. These childObjects are stored in a list in the parentObject class. Pages in my wicket application need to access parentObject, so I made it accessible as so:
public Object getParentObject
{
return this.parentObject;
}
And it is retrieved in each page like so:
((MyApplication)Application.get()).getParentObject()
The problem currently is that the timertask for both the parentObject and childObjects are no longer being called every minute as they should be. My logs pick up the first start of the parentObject, but the logging message is never outputted again signalling that the run() method of parent Object's timertask is not being executed every minute.
EDIT: Added some more code to make it more clear the issue
public class childObject implements Serializable
{
private transient NamedParameterJdbcTemplate njt;
private transient Timer timer;
public childObject(DataSource ds)
{
this.njt = new NamedParamterJdbcTemplate(ds);
}
public void start()
{
timer = new Timer();
timer.schedule(new TimerTask(){
public void run()
{
//do some stuff that is never happening
}
}, 0, 60000);
}
}
public class ParentObject implements Serializable
{
private DataSource ds;
private List<ChildObject> childObjects;
private transient Timer;
public ParentObject(DataSource ds)
{
this.ds = ds;
//add some stuff to childObjects
timer = new Timer();
timer.schedule(new TimerTask(){
public void run()
{
for(some condition)
{
//Do some stuff
if(/*condition is met*/)
{
childObjects.get(i).start();
}
}
}
}, 0, 60000);
}
}
public MyApplication extends WebApplication
{
private ParentObject object;
private DataSource ds;
public void init()
{
super.init();
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
ds = (DataSource) context.getBean("dataSource");
parentObject = new ParentObject(ds);
}
}
Upvotes: 1
Views: 1354
Reputation: 3692
Given your problem description I figure you bind your ParentObject directly to your components instead of binding a reference to the ParentObject. The serialization/deserialization of session scoped objects (which all pages, components and models in effect are) creates these 'new' instances (even though they are deserialized copies of the original parent object).
While you have not shown much code, I suspect you do something like this:
public MyPage() {
Object parentObject = ((MyApplication)Application.get()).getParentObject();
add(new Label("timer", new PropertyModel(parentObject, "time")));
}
This binds the parent object to the property model, and that binds the parent object to the page (through the label component). What you should do instead is:
public MyPage() {
add(new Label("timer", new PropertyModel(this, "application.parentObject.time")));
}
This binds the page to itself, and instructs the property model to retrieve the application dynamically, and from the application the parent object, and from that the time. This way you don't bind your objects to the page hierarchy, but retrieve them dynamically.
Another option is to use a LoadableDetachableModel that retrieves the parent object from the application, and detaches it after rendering the page.
public class ParentObjectModel extends LoadableDetachableModel<ParentObject> {
@Override public ParentObject load() {
return ((MyApplication)Application.get()).getParentObject();
}
}
See the Wicket wiki for more information on Models.
Upvotes: 3