package openjms.examples.nuix;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
import java.util.Properties;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import org.exolab.jms.util.CommandLine;
/**
* The controller for the upload process.
*/
public class Controller extends BaseJmsSenderReceiver {
//////////////////////////////////////////////////////////////////////////////////////
// Constants
/**
* Lock object for synchronisation.
*/
public static final String LOCK = "controller lock.";
public static final String MULTIPLE_STAGES = "multiplestages.selection";
public static final String MULTIPLE_STAGES_VALUE = "multispestages.value";
//////////////////////////////////////////////////////////////////////////////////////
// Fields
/**
* The STAGES table keyed by id.
*/
private Map stages = null;
//////////////////////////////////////////////////////////////////////////////////////
// Mainline
public static void main(String[] args)
throws Exception {
CommandLine line = new CommandLine(args);
if (line.exists("props")) {
Properties props = new Properties(System.getProperties());
props.load(new BufferedInputStream(new FileInputStream(line.value("props"))));
System.setProperties(props);
Controller controller = new Controller();
while(true);
} else {
System.err.println("No property file specified");
System.exit(0);
}
}
//////////////////////////////////////////////////////////////////////////////////////
// Constructors
/**
* Default constructor requires the configuration.
* @param config the configuration to use.
*/
public Controller()
{
super();
try {
// Setup the datasource manager.
//DataSource ds = (DataSource) getDSContext().lookup("DefaultDS");
// Second parameter set to true as we are managing the transactions ourselves.
//dsMgr = new DataSourceManager(ds, true);
controllerInit();
super.init();
}
// If failed while init then we need to shut down system, the server *must*
// run OK before anything else.
catch (Exception e) {
System.err.println("Serious problem with controller, die");
e.printStackTrace();
System.exit(-1);
}
}
//////////////////////////////////////////////////////////////////////////////////////
// Methods
/**
* Do initialisation for the Controller. Set up the following:
* <ul>
* <li>The connection timeout</li>
* <li>Bind to the specified JMS Queue.</li>
* <li>Setup the DB connection pool. </li>
* <li>Cache the STAGES table. </li>
* </ul>
*/
private void controllerInit()
{
}
/**
* Cache the STAGE table from the database.
* @return a map of StageInfo objects keyed by id.
*/
private Map setUpStages() throws Exception
{
return null;
}
/**
* Receive a message from the incoming messages queue and process it.
* @param message
*/
public void onMessage(Message message)
{
try {
// Check if it is an ObjectMessage.
if (message instanceof ObjectMessage) {
// Handle the SME.
handleSMEMessage((ObjectMessage)message);
if (ack == Session.CLIENT_ACKNOWLEDGE) {
System.err.println("Acking message using client_ack");
message.acknowledge();
}
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
}
}
/**
* Handle the actual SMEMessage passed in.
* @param sme the message to process.
*/
private void handleSMEMessage(ObjectMessage sme)
throws Exception
{
processMessage(sme);
}
private void processMessage(ObjectMessage sme) throws Exception
{
Long value = (Long)sme.getObject();
if (value.longValue() < 3) {
sendMessage(new Long(value.longValue() + 1));
}
}
/**
* Exit this JMS sender and receiver gracefully.
*/
protected void exit()
{
super.exit();
System.exit(0);
}
}