-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
224 lines (183 loc) · 8.19 KB
/
Bank.java
File metadata and controls
224 lines (183 loc) · 8.19 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.ibm.mq.samples.jms;
import java.io.Console;
import java.util.*;
import javax.jms.Destination;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.TextMessage;
import javax.swing.text.DefaultStyledDocument.ElementSpec;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;
public class Bank {
static final String HOST = "localhost"; // Host name or IP address
static final int PORT = 1414; // Listener port for your queue manager
static final String CHANNEL = "DEV.APP.SVRCONN"; // Channel name
static final String QMGR = "QM1"; // Queue manager name
static final String APP_USER = "app"; // User name that application uses to connect to MQ
static final String APP_PASSWORD = "passw0rd"; // Password that the application uses to connect to MQ
static JmsFactoryFactory ff = null;
static JmsConnectionFactory connectionFactory = null;
static HashMap<String,String> activeConnections = new HashMap<String, String>(){{
put("DEV.QUEUE.4", "");
put("DEV.QUEUE.3", "");
put("DEV.QUEUE.2", "");
put("DEV.QUEUE.1", "");
}};
static int numConnections = 0;
static HashMap<String, Integer> balances = new HashMap<String, Integer>(){{
put("Bob", 10000);
put("Alice", 500);
put("John", 30000);
}};
public static void main(String args[]){
try {
ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
connectionFactory = ff.createConnectionFactory();
}catch(Exception e){
System.out.println(e);
}
try {
System.out.println("Welcome to Walker's Bank. Use the ATM's to interact.");
//Set up Publisher connection
IBM_MQ_QUEUE publisher = new IBM_MQ_QUEUE("dev/", "Publisher")
//Set up CLI thread to run
CommandLineInterpreter CLI = new CommandLineInterpreter(publisher);
Thread CLI_thread = new Thread(CLI);
CLI_thread.start();
//Set up queue 1 for accepting new connections
IBM_MQ_QUEUE queue1 = new IBM_MQ_QUEUE("DEV.QUEUE.1", "NewConnectionsProcesser");
//Constantly listen on queue1 for incoming ATM connections
for(;;){
String recievedMessage = queue1.consumer.receiveBody(String.class, 100000000);
if(!recievedMessage.contains("#")){ //Edge case where one of banks messages gets caught in queue. if message is a DEV.QUEUE.X one, skip it
System.out.println("Skip this one");
continue;
}
System.out.println("[NEW ATM CONNECTION]: " + recievedMessage);
//Tell ATM to meet on new Queue
String message = "DEV.QUEUE."+(Bank.numConnections+1);
queue1.producer.send(queue1.destination, message);
System.out.println("Sent message: " + message);
//Start new thread for communication over DEV.QUEUE.X with new ATM
BankToATM newATM = new BankToATM("DEV.QUEUE." + (Bank.numConnections+1), recievedMessage);
Thread newATM_Thread = new Thread(newATM);
newATM_Thread.start();
}
//recordSuccess();
} catch (Exception e) {
System.out.println(e);
}
}
}
class IBM_MQ_QUEUE {
//static final String QUEUE_NAME = "DEV.QUEUE.1"; // Queue that the application uses to put and get messages to and from
JMSContext context = null;
Destination destination = null;
JMSProducer producer = null;
JMSConsumer consumer = null;
String queue_name;
public IBM_MQ_QUEUE(String queue_name, String description){
// Variables
this.context = null;
this.destination = null;
this.producer = null;
this.consumer = null;
this.queue_name = queue_name;
Bank.activeConnections.put(queue_name, description);
if(!queue_name.equals("dev/")){
Bank.numConnections++;
}
try {
// Set the properties
Bank.connectionFactory.setStringProperty(WMQConstants.WMQ_HOST_NAME, Bank.HOST);
Bank.connectionFactory.setIntProperty(WMQConstants.WMQ_PORT, Bank.PORT);
Bank.connectionFactory.setStringProperty(WMQConstants.WMQ_CHANNEL, Bank.CHANNEL);
Bank.connectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
Bank.connectionFactory.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, Bank.QMGR);
Bank.connectionFactory.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "Bank (JMS)");
Bank.connectionFactory.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
Bank.connectionFactory.setStringProperty(WMQConstants.USERID, Bank.APP_USER);
Bank.connectionFactory.setStringProperty(WMQConstants.PASSWORD, Bank.APP_PASSWORD);
//Bank.connectionFactory.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "*TLS12ORHIGHER");
// Create JMS objects
context = Bank.connectionFactory.createContext();
if(queue_name.equals("dev/")){
destination = context.createTopic("dev/");
}else{
destination = context.createQueue("queue:///" + queue_name);
}
//create producer
producer = context.createProducer();
//create consumer (Unless we're making the publisher topic. Don't need consumer in that case.)
if(!queue_name.equals("dev/")){
consumer = context.createConsumer(destination); // autoclosable
}
System.out.println("CONNECTED to " + queue_name);
} catch(Exception e){
System.out.print(e);
}
}
public void close(){
System.out.println("DISCONNECTED from " + queue_name + ".");
context.close();
}
}
class BankToATM implements Runnable{
IBM_MQ_QUEUE queue;
public BankToATM(String queue, String description){
this.queue = new IBM_MQ_QUEUE(queue, description);
}
@Override
public void run(){
//Listen for messages from ATM#XXX (withdraw/deposit queries)
for(;;){
String receivedMessage = queue.consumer.receiveBody(String.class, 100000000);
System.out.println("Received message: " + receivedMessage);
String[] parts = receivedMessage.split(" ");
String n = "new ";
//Interpret messages. Ex) "withdraw Bob 500" or "deposit Alice 20"
if(parts.length == 5 && parts[2].equals("withdraw")){
Bank.balances.put(parts[3], (Bank.balances.get(parts[3]) - Integer.parseInt(parts[4])));
System.out.println("Withdrew $" + parts[4] + " from " + parts[3] +"'s account.");
}else if(parts.length == 5 && parts[2].equals("deposit")){
Bank.balances.put(parts[3], (Bank.balances.get(parts[3]) + Integer.parseInt(parts[4])));
System.out.println("Deposited $" + parts[4] + " into " + parts[3] +"'s account.");
}else if(parts.length == 4 && parts[2].equals("balance")){
System.out.println("Balance check for " + parts[3]+ ": $" + Bank.balances.get(parts[3]));
n = "";
}else{
System.out.println("Invalid Bank Query");
}
}
}
//Helper function to sleep for some time
public static void wait(int ms){
try{
Thread.sleep(ms);
}catch(InterruptedException ex){
Thread.currentThread().interrupt();
}
}
}
class CommandLineInterpreter implements Runnable{
IBM_MQ_QUEUE publisher;
public CommandLineInterpreter(IBM_MQ_QUEUE publisher){
this.publisher = publisher;
}
@Override
public void run(){
//Read in input from Console and either print status or send a publish message to ATM's
for(;;){
String input = System.console().readLine().trim();
if(input.equals("status")){
System.out.println("\nConnections: " + Bank.activeConnections.toString());
System.out.println("Balances: " + Bank.balances + "\n");
}else{
publisher.producer.send(publisher.destination, input);
}
}
}
}