简体   繁体   English

小门和多线程业务对象

[英]Wicket And Multi-threaded Business Object

So I have somewhat limited experience with serialization, Wicket, and multi thread projects so bear with me. 因此,我在序列化,Wicket和多线程项目方面的经验有限,请耐心等待。

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. 本质上,我的Web应用程序类是实例化一个POJ(父对象),该POJ创建一个新的计时器并实例化几个还带有定时器的POJ(子对象)。 These childObjects are stored in a list in the parentObject class. 这些childObjects存储在parentObject类的列表中。 Pages in my wicket application need to access parentObject, so I made it accessible as so: 我的检票口应用程序中的页面需要访问parentObject,因此我可以这样访问它:

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. 当前的问题是,parentObject和childObjects的timertask不再每分钟都应该被调用。 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. 我的日志记录了parentObject的第一个开始,但是从未再次输出日志消息,这表明父Object的timertask的run()方法没有每分钟执行一次。 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);
    }
}

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. 对象本身永远不会附加到检票口组件上,尽管属于其类成员的变量是String,integer,Date。

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. Wicket基本上是单线程的(由于难以正确实现多线程,大多数优秀的GUI框架也是如此),因此应避免实例化任务。 (Marking the Timer as transient will mean it gets lost upon deserialization by the way, which might be the cause of your problems) (将Timer标记为瞬态将意味着它会在反序列化时丢失,这可能是造成问题的原因)

You should rearchitect your application to have a service layer which is accessed by Wicket components on demand, possibly using LoadableDetachableModels. 您应该重新设计您的应用程序以具有服务层,Wicket组件可以根据需要使用ServiceableDetachableModels访问该服务层。 The service layer can have tasks and so on, as it will be managed by Spring rather than Wicket. 服务层可以包含任务等,因为它将由Spring而不是Wicket进行管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM