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