import ch.softwired.ibus.Channel;
import ch.softwired.ibus.ChannelURL;
import ch.softwired.ibus.Subscriber;
import ch.softwired.ibus.PublishListener;
import ch.softwired.ibus.PublishEvent;
import ch.softwired.ibus.util.ThreadHelper;

public class StockQuoteConsumer implements PublishListener {

	public static void main(String [] args) throws Exception {
		//First, we need an instance of our class which will receive
		//all the stock quotes via its handlePublishEvent() method
		StockQuoteConsumer consumer = new StockQuoteConsumer();
		
		//We have now an instance of the PublishEvent listener.
		//In addition, we need an instance of the
		//Event source: Subscriber
		Subscriber s = new Subscriber();

		//Before we can register the event listener with the event
		//source, the event source must know it's channel.
		//So, get us a channel
		Channel blueChipsZRH = new Channel();
		
		//This channel cannot be used before setting its
		//ChannelURL property
		//So, create an URL first...
		ChannelURL url = new
		    ChannelURL("ibus:///quotes/zrh");
		
		//...and use it to set the Channel's ChannelURL property
		blueChipsZRH.setChannelURL(url);
		
		//...and use the now working Channel to set the Subscriber's
		//channel property
		s.setChannel(blueChipsZRH);
		
		//At last, the event listener can be registered with
		//the event source
		s.addPublishListener(consumer);

		//That's it, the consumer should get the events now
		
		//Since iBus only creates "daemon" threads, we must keep
		//our application running by ourselves.
		ThreadHelper.WAIT_UNTIL_EXIT.suspend();
	}

	public void handlePublishEvent(PublishEvent event) {
		//Pull out the quote from the event object
		String quote = (String)event.getObject();
		System.out.println("Got a stock quote: " + quote);
	}
}
