小门和多线程业务对象 [英] Wicket And Multi-threaded Business Object

查看:82
本文介绍了小门和多线程业务对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在序列化,Wicket和多线程项目方面的经验有限,所以请耐心等待.

So I have somewhat limited experience with serialization, Wicket, and multi thread projects so bear with me.

本质上,我的Web应用程序类正在实例化一个POJ(父对象),该POJ创建一个新的计时器,并实例化几个还具有定时器的POJ(子对象).这些childObjects存储在parentObject类的列表中.我的检票口应用程序中的页面需要访问parentObject,因此我可以这样访问它:

Essentially my web application class is instantiating a POJ (parentObject) which creates a starts a new timer and instantiates several POJs (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()

当前的问题是,parentObject和childObjects的timertask不再每分钟都应该被调用.我的日志记录了parentObject的第一个开始,但是不再输出日志消息,这表明父Object的timertask的run()方法没有每分钟执行一次.子对象也是如此.似乎计时器只执行一次.以下是我所拥有的一些伪代码

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. The same holds true for the child Objects. It seems like the timers are only being executed once. Below is some pseudocode for what I have

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*/)
                    {
                             //starts the child's timer to do stuff
                        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);
    }
}

我什至需要使这些对象可序列化吗?对象本身永远不会附加到检票口组件上,尽管属于其类成员的变量是String,integer,Date.

Do I even need to make these objects Serializable? The objects themselves are never being attached to wicket components, although String, integer, Date sorts of variables that are members of their classes are.

推荐答案

Wicket基本上是单线程的(由于很难正确地实现多线程,大多数优秀的GUI框架也是如此),并且应该避免实例化任务. (将Timer标记为瞬态将意味着它在反序列化时会丢失,这可能是造成问题的原因)

Wicket is fundamentally single threaded (as are most good GUI frameworks due to the difficulty of getting multithreading right) and you should avoid instantiating tasks. (Marking the Timer as transient will mean it gets lost upon deserialization by the way, which might be the cause of your problems)

您应该重新设计您的应用程序,使其具有一个服务层,Wicket组件可以根据需要使用ServiceableDetachableModels访问该服务层.服务层可以包含任务等,因为它将由Spring而不是Wicket管理.

You should rearchitect your application to have a service layer which is accessed by Wicket components on demand, possibly using LoadableDetachableModels. The service layer can have tasks and so on, as it will be managed by Spring rather than Wicket.

这篇关于小门和多线程业务对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆