Vaadin中的多线程刷新UI [英] Multithreading refresh UI in Vaadin
问题描述
我正在尝试在我的Vaadin应用程序中实现多线程刷新UI.
I'm trying to implement a multithreading refresh UI in my Vaadin app.
此UI的一部分是基于容器的dChart
.要构建dChart
遍历容器并通过元素的属性之一对元素进行计数,如下所示:
Part of this UI is a dChart
based on container. To build dChart
Im iterating through container and count elements by one of their properties, like this:
Collection<?> itemIDS = container.getItemIds();
for (Object itemID : itemIDS) {
Property property = container.getContainerProperty(itemID, "status");
String status = (String) property.getValue();
if (countMap.containsKey(status)) {
countMap.put(status, countMap.get(status) + 1);
} else {
countMap.put(status, 1);
}
}
但是,如果容器包含数千个元素,则需要花费2-3秒的时间.
However it takes over 2-3 seconds if container has thousands of elements.
用户只是迫不及待地想刷新UI.
User just can't wait so long to refresh an UI.
我读到我可以构建我的UI,然后在完全构建dChart之后使用@Push
Vaadin注释刷新它.
I read that i can build my UI and later just refresh it using @Push
Vaadin annotation, after dChart is fully built.
所以我建立了这样的东西:
So i build something like this:
{
//class with @Push
void refreshPieDChart(GeneratedPropertyContainer container) {
new InitializerThread(ui, container).start();
}
class InitializerThread extends Thread {
private LogsUI parentUI;
private GeneratedPropertyContainer container;
InitializerThread(LogsUI ui, GeneratedPropertyContainer container) {
parentUI = ui;
this.container = container;
}
@Override
public void run() {
//building dChart etc etc... which takes over 2-3 seconds
// Init done, update the UI after doing locking
parentUI.access(new Runnable() {
@Override
public void run() {
chart.setDataSeries(dataSeries).show();
}
});
}
}
}
但是,如果我刷新页面几次,则会生成有关SQLContainer
的错误:
However if i refresh page few times, it is generating errors about SQLContainer
:
java.lang.IllegalStateException: A trasaction is already active!
由于几次刷新后,我的多个线程正在使用同一SQLContainer并行运行.
Becouse after few refreshes, my multiple threads are running parallel using the same SQLContainer.
要解决此问题,我想停止除最后一个线程之外的所有工作刷新线程,以消除并发问题.我该怎么办?也许其他解决方案?
To fix that, I want to stop all working refresh threads except last one to eliminate concurrent problem. How i can do it? Maybe other solution?
我已经尝试过这样的方法,但是问题仍然存在,这是防止并发问题的正确方法吗?
I have tried smth like this, but problem still remains, is it a correct way to prevent concurrent problem?
{
private static final Object mutex = new Object();
//class with @Push
void refreshPieDChart(GeneratedPropertyContainer container) {
new InitializerThread(ui, container).start();
}
class InitializerThread extends Thread {
private LogsUI parentUI;
private GeneratedPropertyContainer container;
InitializerThread(LogsUI ui, GeneratedPropertyContainer container) {
parentUI = ui;
this.container = container;
}
@Override
public void run() {
//is it correct way to prevent concurrent problem?
synchronized(mutex){
//method to refresh/build chart which takes 2-3s.
}
// Init done, update the UI after doing locking
parentUI.access(new Runnable() {
@Override
public void run() {
chart.setDataSeries(dataSeries).show();
}
});
}
}
}
推荐答案
这就是我所做的:
Henri Kerola 为我指出了一个非常明显的主意:用SQL进行计数.正如我所说的那样,但这意味着我需要为每种可能的过滤器组合准备SQL.那是一个相当复杂的过程,看起来并不好.
Henri Kerola points me pretty obvious idea: do counting in SQL. As i said I was thinking about that but that would means I need to prepare SQL for every possible filters combination. That is a pretty complicated and doesn't look good.
但是,这使我意识到,如果我对表使用带有过滤器的SQLContainer,则可以执行相同的计数.我只需要用自己的FreeformQuery
和FreeformStatementDelegate
创建第二个SQLContainer
.
But this realised me that if I'm using SQLContainer with filters for my table, I can do the same for counting. I just need to create second SQLContainer
with my ownFreeformQuery
and FreeformStatementDelegate
.
如果我要创建以上所有内容,则可以将相同"过滤器添加到两个容器中,但是现在我不需要计数元素,因为第二个容器为我保存了值.听起来很完美,但请看一下我的代码:
If i will create all of above, i can just add THE SAME filters to both containers however i dont need to count elements now becouse 2nd container holds values for me. It sounds complecated but take a look on my code:
FreeformQuery myQuery = new MyFreeformQuery(pool);
FreeformQuery countQuery = new CountMyFreeformQuery(pool);
SQLContainer myContainer = new SQLContainer(myQuery); //here i hold my all records as in a Question
SQLContainer countContainer = new SQLContainer(countQuery); //here i hold computed count(*) and status
MyFreeformQuery.java 如下:
class ProcessFreeformQuery extends FreeformQuery {
private static final String QUERY_STRING = "SELECT request_date, status, id_foo FROM foo";
private static final String COUNT_STRING = "SELECT COUNT(*) FROM foo";
private static final String PK_COLUMN_NAME = "id_foo";
MyFreeformQuery(JDBCConnectionPool connectionPool) {
super(QUERY_STRING, connectionPool, PK_COLUMN_NAME);
setDelegate(new AbstractFreeformStatementDelegate() {
protected String getPkColumnName() {
return PK_COLUMN_NAME;
}
protected String getQueryString() {
return QUERY_STRING;
}
protected String getCountString() {
return COUNT_STRING;
}
});
}
和最重要的 CountFreeformQuery.java 如下:
public class CountFreeformQuery extends FreeformQuery {
private static final String QUERY_STRING_GROUP = "SELECT status, count(*) as num FROM foo GROUP BY status";
private static final String QUERY_STRING = "SELECT status, count(*) as num FROM foo";
private static final String GROUP_BY = "foo.status";
public CountFreeformQuery(JDBCConnectionPool connectionPool) {
super(QUERY_STRING_GROUP, connectionPool);
setDelegate(new CountAbstractFreeformStatementDelegate() {
protected String getQueryString() {
return QUERY_STRING;
}
protected String getGroupBy(){
return GROUP_BY;
}
});
}
}
现在,如果我想在这样的情况下刷新dChart
:
Now if i want to refresh dChart
after smth like that:
myContainer.addContainerFilter(new Between(DATE_PROPERTY, getTimestamp(currentDate), getOneDayAfter(currentDate)));
我只是对countContainer
做同样的事情:
I just do the same with countContainer
:
countContainer.addContainerFilter(new Between(DATE_PROPERTY, getTimestamp(currentDate), getOneDayAfter(currentDate)));
并将其传递给不需要计算元素的方法,只需将所有容器添加到映射,然后像这样将其添加到dChart:
And pass it to method which dont need to count elements, just add all container to map and then to dChart like that:
Map<String, Long> countMap = new HashMap<String, Long>();
Collection<?> itemIDS = container.getItemIds();
for (Object itemID : itemIDS) {
Property statusProperty = container.getContainerProperty(itemID, "status");
Property numProperty = container.getContainerProperty(itemID, "num");
countMap.put((String) statusProperty.getValue(), (Long) numProperty.getValue());
}
现在,我已经计算了myContainer
个元素中的status
个元素,无需进行多线程处理或编写大量的sql.
Now i have status
es of elements in myContainer
counted, no need to multithreading or writing tons of sql.
谢谢大家的建议.
这篇关于Vaadin中的多线程刷新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!