-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathQueueSender.java
More file actions
196 lines (157 loc) · 6.17 KB
/
QueueSender.java
File metadata and controls
196 lines (157 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.exit;
import java.net.URISyntaxException;
import java.util.logging.Level;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QueueSender {
private static final Logger LOGGER =
LoggerFactory.getLogger(QueueSender.class);
//private String clientId;
private Connection connection;
private Session session;
private MessageProducer messageProducer;
String un="";
String pw="";
String url="";
private static String QN="";
private static BufferedReader msgstream = new BufferedReader(new InputStreamReader(System.in));
public void init(String queueName)
throws JMSException, URISyntaxException, IOException {
if (un.isEmpty() || pw.isEmpty() || url.isEmpty())
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter the details to Connect to ActiveMQ");
System.out.print("\nEnter the UserName : ");
un=br.readLine();
java.io.Console console = System.console();
if ( console instanceof java.io.Console)
{
System.out.print("Enter the Password : ");
char[] pwd=console.readPassword();
//Convert char array to String
pw = new String(pwd);
}
else
{
System.out.print("Enter the Password : ");
pw=br.readLine();
}
System.out.print("Enter the ConnectionURL : ");
url=br.readLine();
}
else
{
LOGGER.info("Taking Already Entered Username,Password and ActiveMQ URL");
}
if (un.equals(null) || pw.equals(null) || url.equals(null) || un.trim().length() == 0 || pw.trim().length() == 0 || url.trim().length() == 0 || un.equals("") || pw.equals("") || url.equals(""))
{
LOGGER.error("Either Username or Password or BrokerURL is null/empty. Correct it");
exit(2);
}
else
{
connection = ActiveMQConnection.makeConnection(un,pw,url);
}
// create a Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//System.out.print ("what is session"+session);
// create the Queue to which messages will be sent . If the Queue is not there it will be auto created
Queue queue = session.createQueue(queueName);
// create a MessageProducer for sending messages
messageProducer = session.createProducer(queue);
}
public void closeConnection() throws JMSException {
connection.close();
}
public void sendmessage(String Message)
throws JMSException {
String text = Message;
// create a JMS TextMessage
TextMessage textMessage = session.createTextMessage(text);
// send the message to the queue destination
messageProducer.send(textMessage);
LOGGER.info("sent message with text='{}'", text);
}
public static void main(String[] args) throws IOException, JMSException, URISyntaxException
{
System.out.println("-------------------------------");
System.out.println("ACTIVEMQ - QUEUE SENDER - CLI");
System.out.println("-------------------------------");
String line="";
QueueSender sobj = new QueueSender();
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
try {
boolean quitNow = false;
do {
System.out.print("\nEnter the Queue Name (or) quit/Quit to Exit ["+ QN +"] :");
String QNI = msgstream.readLine();
QNI = QNI.trim();
if ( QNI == "" || QNI.equals(null) || QNI.trim().length() == 0 && QN.isEmpty())
{
LOGGER.error("Queue name is Invalid, Please Enter Valid QueueName");
}
else if ( QNI == "" || QNI.equals(null) || QNI.trim().length() == 0 )
{
LOGGER.info("Taking the Already Entered QueueName - "+QN);
}
else
{
QN=QNI;
}
if( QN.equalsIgnoreCase("Quit"))
{
String ans;
int choice;
System.out.print("You have entered Quit, We are not sure if that's Queue Name.");
System.out.print ("\n\nPlease Clarify? \n1 => Press One to Exit \n2 => Press 2 to Reset/Continue \n3 => Quit is my Queue Name\n\nYour Option :");
ans=msgStream.readLine();
choice=Integer.parseInt(ans);
switch (choice)
{
case 1: quitNow=true;
System.out.println("GoodBye!");
System.exit(0);
break;
case 2: quitNow=false;
break;
case 3: quitNow=false;
sobj.init(QN);
System.out.print("\nEnter the message to send: \n");
line = msgStream.readLine();
sobj.sendmessage(line);
LOGGER.info("JMS Message Sent: "+line+"\n");
break;
default:
LOGGER.error("No Such Option Sorry! Try Again");
quitNow=false;
break;
}
}
else
{
sobj.init(QN);
System.out.print("\nEnter the message to send: \n");
line = msgStream.readLine();
sobj.sendmessage(line);
sobj.closeConnection();
}
}
while(! quitNow);
sobj.closeConnection();
} catch (JMSException ex) {
java.util.logging.Logger.getLogger(QueueSender.class.getName()).log(Level.SEVERE, null, ex);
}
}
}