(Given below is the rest of the code related to the above problem.)
**InstrumentManagerImpl Class**
Code:
package com.xxxxx.admin.instrument;
import java.math.BigDecimal;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import com.xxxxx.admin.account.AccountManager;
import com.xxxxx.admin.dao.InstrumentDAO;
import com.xxxxx.admin.entity.Instrument;
import com.xxxxx.admin.entity.Account;
import com.xxxxx.admin.entity.Portfolio;
import com.xxxxx.admin.entity.Users;
import com.xxxxx.admin.portfolio.PortfolioManager;
import com.xxxxx.admin.user.UserManager;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.util.Logger;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;
import org.directwebremoting.Browser;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.impl.DaemonThreadFactory;
import org.directwebremoting.ui.dwr.Util;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class InstrumentManagerImpl implements InstrumentManager,Runnable {
private final static String SCRIPT_SESSION_ATTR = "SCRIPT_SESSION_ATTR";
public static String getScriptSessionAttr() {
return SCRIPT_SESSION_ATTR;
}
private UserManager userManager;
public UserManager getUserManager() {
return userManager;
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
private InstrumentDAO instrumentDAO;
public InstrumentDAO getInstrumentDAO() {
return instrumentDAO;
}
public void setInstrumentDAO(InstrumentDAO instrumentDAO) {
this.instrumentDAO = instrumentDAO;
}
public InstrumentManagerImpl() {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory());
executor.scheduleAtFixedRate(this, 1, 10, TimeUnit.SECONDS);
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run()
{
SendInstrumentSymbols();
}
/*used to implement the find() functionality*/
@Override
public List<Instrument> findInstrumentSymbols() {
return instrumentDAO.findInstrumentSymbols();
}
/*used to implement the reverse ajax functionality*/
public void SendInstrumentSymbols() {
// Get the current page.
String page = ServerContextFactory.get().getContextPath() + "/faces/gehan.xhtml";
// Create a new AttributeScriptSessionFilter which will look for an attribute on the ScriptSession
ScriptSessionFilter attributeFilter = new AttributeScriptSessionFilter(SCRIPT_SESSION_ATTR);
// Update the page, filters ScriptSessions using attributeFilter. If the SCRIPT_SESSION_ATTR
// has not been set on the ScriptSession the page in question will not receive updates.
Browser.withPageFiltered(page, attributeFilter, new Runnable()
{
@Override
public void run()
{
List<Instrument> InstrumentList = new ArrayList<Instrument>();
InstrumentList = findInstrumentSymbols() ;
//Creates a multi-dimensional array, containing a row and the rows column data.
//Added the First Element of the List returned - values are converted to String
String[][] data = {
{InstrumentList.get(0).getSymbolName().toString(), InstrumentList.get(0).getName().toString(), String.valueOf(InstrumentList.get(0).getQuantity()), InstrumentList.get(0).getPrice().toString(), String.valueOf(InstrumentList.get(0).getMarketMakingLimit())}};
// Call DWR's util which adds rows into a table.peopleTable is the id of the tbody and
data contains the row/column data.
Util.addRows("MMTable", data);
}
});
}
/**
* Called from the client to add an attribute on the ScriptSession. This
* attribute will be used so that only pages (ScriptSessions) that have
* set this attribute will be updated.
*/
public void addAttributeToScriptSession() {
ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
scriptSession.setAttribute(SCRIPT_SESSION_ATTR, true);
}
/**
* Called from the client to remove an attribute from the ScriptSession.
* When called from a client that client will no longer receive updates
* (unless addAttributeToScriptSession)
* is called again.
*/
public void removeAttributeToScriptSession() {
ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
scriptSession.removeAttribute(SCRIPT_SESSION_ATTR);
}
/**
* This is the ScriptSessionFilter that will be used to filter out all ScriptSessions
* unless they contain the SCRIPT_SESSION_ATTR attribute.
*/
protected class AttributeScriptSessionFilter implements ScriptSessionFilter
{
private final String attributeName;
public AttributeScriptSessionFilter(String attributeName)
{
this.attributeName = attributeName;
}
@Override
public boolean match(ScriptSession session)
{
Object check = session.getAttribute(attributeName);
return (check != null && check.equals(Boolean.TRUE));
}
}
}