import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class PSProducer {
	public static void main(String[] args) {
		Topic t = null;
		TopicConnectionFactory tcf = null;
		TopicConnection tc = null;

		try {
			Context jndiContext = new InitialContext();
			tcf = (TopicConnectionFactory) jndiContext.lookup("jms/TopicConnectionFactory");
		} catch (NamingException ne) {
			System.out.println(ne+ " does not exist");
			System.exit(1);
		}

		try {
			tc = tcf.createTopicConnection();
			TopicSession ts = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
			t = ts.createTopic("StockQuote");

			TopicPublisher tp = ts.createPublisher(t);

			TextMessage msg1 = ts.createTextMessage();
			TextMessage msg2 = ts.createTextMessage();

			msg1.setText("Quote");
			msg1.setStringProperty("Company","DCX");
			msg1.setDoubleProperty("USD",41.67);
			msg1.setDoubleProperty("EUR",34.99);
			msg1.setStringProperty("Date","2004-05-23");

			tp.publish(msg1, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, 7*24*3600*1000L);

			try {
				Thread.sleep(30000);
			} catch(InterruptedException ie) {
				System.err.println("InterruptedException caught");
			}

			msg2.setText("Quote");
			msg2.setStringProperty("Company","AZ");
			msg2.setDoubleProperty("USD",10.04);
			msg2.setDoubleProperty("EUR",9.30);
			msg2.setStringProperty("Date","2004-05-23");

			tp.publish(msg2, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, 7*24*3600*1000L);
		} catch (JMSException e) {
			System.err.println("JMSException caught");
			e.printStackTrace();
		}
	}
}