1 package ejava.examples.jmsnotifier;
2
3 import javax.jms.Connection;
4 import javax.jms.ConnectionFactory;
5 import javax.jms.Destination;
6 import javax.jms.MessageProducer;
7 import javax.jms.Session;
8 import javax.jms.TextMessage;
9 import javax.naming.Context;
10 import javax.naming.InitialContext;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15
16
17
18
19
20
21 public class Publisher implements Runnable {
22 private static final Log log = LogFactory.getLog(Publisher.class);
23 protected ConnectionFactory connFactory;
24 protected Destination destination;
25 protected boolean stop = false;
26 protected boolean stopped = false;
27 protected boolean started = false;
28 protected int count=0;
29 protected String name;
30 protected long sleepTime=10000;
31 protected int maxCount=10;
32 protected String username;
33 protected String password;
34
35 public Publisher(String name) {
36 this.name = name;
37 }
38 public void setConnFactory(ConnectionFactory connFactory) {
39 this.connFactory = connFactory;
40 }
41 public void setDestination(Destination destination) {
42 this.destination = destination;
43 }
44 public int getCount() {
45 return count;
46 }
47 public void setSleepTime(long sleepTime) {
48 this.sleepTime = sleepTime;
49 }
50 public void setMaxCount(int maxCount) {
51 this.maxCount = maxCount;
52 }
53 public void clearMessages() {
54 count = 0;
55 }
56 public void stop() {
57 this.stop = true;
58 }
59 public boolean isStopped() {
60 return stopped;
61 }
62 public boolean isStarted() {
63 return started;
64 }
65 public void setUsername(String username) {
66 this.username = username;
67 }
68 public void setPassword(String password) {
69 this.password = password;
70 }
71
72 public void execute() throws Exception {
73 Connection connection = null;
74 Session session = null;
75 MessageProducer producer = null;
76 try {
77 connection = username==null ?
78 connFactory.createConnection() :
79 connFactory.createConnection(username, password);
80 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
81 producer = session.createProducer(destination);
82 stopped = stop = false;
83
84 log.info("publisher " + name + " starting: " +
85 "maxCount=" + maxCount +
86 ", sleepTime" + sleepTime);
87 started = true;
88 while (!stop && (maxCount==0 || count < maxCount)) {
89 TextMessage message = session.createTextMessage();
90 message.setIntProperty("count", ++count);
91 message.setText("count = " + count);
92 producer.send(message);
93 log.debug("published message(" + count + "):" +
94 message.getJMSMessageID());
95 Thread.sleep(sleepTime);
96 }
97 log.info("publisher " + name + " stopping, count=" + count);
98 connection.stop();
99 }
100 finally {
101 stopped = true;
102 started = false;
103 if (producer != null) { producer.close(); }
104 if (session!=null){ session.close();}
105 if (connection != null) { connection.close(); }
106 }
107 }
108
109 public void run() {
110 try {
111 execute();
112 }
113 catch (Exception ex) {
114 log.fatal("error running " + name, ex);
115 }
116 }
117
118 public static void main(String args[]) {
119 boolean noExit=false;
120 try {
121 System.out.print("Publisher args:");
122 for (String s: args) {
123 System.out.print(s + " ");
124 }
125 System.out.println();
126 String connFactoryJNDI=null;
127 String destinationJNDI=null;
128 String name="";
129 Long sleepTime=null;
130 Integer maxCount=null;
131 String username=null;
132 String password=null;
133 for (int i=0; i<args.length; i++) {
134 if ("-jndi.name.connFactory".equals(args[i])) {
135 connFactoryJNDI = args[++i];
136 }
137 else if ("-jndi.name.destination".equals(args[i])) {
138 destinationJNDI=args[++i];
139 }
140 else if ("-name".equals(args[i])) {
141 name=args[++i];
142 }
143 else if ("-sleep".equals(args[i])) {
144 sleepTime=new Long(args[++i]);
145 }
146 else if ("-max".equals(args[i])) {
147 maxCount=new Integer(args[++i]);
148 }
149 else if ("-username".equals(args[i])) {
150 username=args[++i];
151 }
152 else if ("-password".equals(args[i])) {
153 password=args[++i];
154 }
155 else if ("-noExit".equals(args[i])) {
156 noExit=true;
157 }
158 }
159 if (connFactoryJNDI==null) {
160 throw new Exception("jndi.name.connFactory not supplied");
161 }
162 else if (destinationJNDI==null) {
163 throw new Exception("jndi.name.destination not supplied");
164 }
165 Publisher publisher = new Publisher(name);
166 Context jndi = new InitialContext();
167 publisher.setConnFactory(
168 (ConnectionFactory)jndi.lookup(connFactoryJNDI));
169 publisher.setDestination((Destination)jndi.lookup(destinationJNDI));
170 if (maxCount!=null) {
171 publisher.setMaxCount(maxCount);
172 }
173 if (sleepTime!=null) {
174 publisher.setSleepTime(sleepTime);
175 }
176 publisher.setUsername(username);
177 publisher.setPassword(password);
178 publisher.execute();
179 }
180 catch (Exception ex) {
181 log.fatal("",ex);
182 if (noExit) {
183 throw new RuntimeException("error in publisher", ex);
184 }
185 System.exit(-1);
186 }
187 }
188
189
190 }