1 46 47 48 49 package sample.jms.files; 50 51 import java.io.IOException ; 52 53 import javax.jms.QueueConnectionFactory ; 54 import javax.jms.QueueSession ; 55 import javax.jms.Session ; 56 57 import org.mr.api.jms.MantaFileMessage; 58 import org.mr.api.jms.MantaQueueConnectionFactory; 59 60 61 65 66 67 public class FileTransfer implements javax.jms.MessageListener { 68 private static final int MESSAGE_TTL = 6000000; 69 70 private javax.jms.QueueConnection con = null; 71 private javax.jms.QueueSession sendSession = null; 72 private javax.jms.QueueSession receiveSession = null; 73 private javax.jms.QueueSender sender = null; 74 75 private MantaFileMessage fileInPipe; 76 77 78 private void transfer(String userName, String rQueue, String sQueue) 79 { 80 try 82 { 83 QueueConnectionFactory conFactory = (QueueConnectionFactory ) new MantaQueueConnectionFactory(); 84 85 con = conFactory.createQueueConnection(); 87 88 sendSession =(QueueSession ) con.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 90 receiveSession =(QueueSession ) con.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 91 92 } 93 catch (javax.jms.JMSException jmse) 94 { 95 jmse.printStackTrace(); 96 waitForAnyKey(); 97 98 try { 99 System.in.read(); 100 } catch (IOException e) { 101 e.printStackTrace(); 102 } 103 104 System.exit(1); 105 } 106 107 try 109 { 110 if (sQueue != null) 111 { 112 javax.jms.Queue sendQueue = sendSession.createQueue (sQueue); 113 sender = sendSession.createSender(sendQueue); 114 } 115 if (rQueue != null) 116 { 117 javax.jms.Queue receiveQueue = receiveSession.createQueue (rQueue); 118 javax.jms.QueueReceiver qReceiver = receiveSession.createReceiver(receiveQueue); 119 qReceiver.setMessageListener(this); 120 System.out.println ("\nStart receiving messages on queue \"" + rQueue + "\".\n"); 123 con.start(); 124 } 125 } 126 catch (javax.jms.JMSException jmse) 127 { 128 jmse.printStackTrace(); 129 exit(); 130 } 131 try 133 { 134 java.io.BufferedReader stdin = 136 new java.io.BufferedReader ( new java.io.InputStreamReader ( System.in ) ); 137 if (sQueue != null){ 138 System.out.println("Enter the file to send to queue \"" + sQueue + "\"."); 139 System.out.println("Press Enter to send the file."); 140 141 System.out.println("Typing 'exit' will stop the program."); 142 } 143 144 while ( true ) 145 { 146 System.out.print(userName+">"); 147 String s = stdin.readLine(); 148 149 if(s == null) 150 continue; 151 s =s.trim(); 153 154 if(s.length() ==0) 155 continue; 156 157 if ( s.equalsIgnoreCase("exit")) 158 exit(); 159 160 else if ( s.startsWith("upload")&& sQueue != null) 161 { 162 s = s.substring(s.indexOf("upload")+6); 163 s = s.trim(); 164 if(s.length() == 0) 165 continue; 166 MantaFileMessage msg = new MantaFileMessage(); 167 msg.load(s); 168 sender.send( msg, 171 javax.jms.DeliveryMode.NON_PERSISTENT, 172 javax.jms.Message.DEFAULT_PRIORITY, 173 MESSAGE_TTL); 174 }else if ( s.startsWith("save")&& sQueue != null) 175 { 176 s = s.substring(s.indexOf("save")+4); 177 s = s.trim(); 178 if(s.length() == 0) 179 continue; 180 if(fileInPipe == null){ 181 System.out.println( "No file is waiting to be saved" ); 182 continue; 183 } 184 fileInPipe.save(s); 185 fileInPipe = null; 186 }else{ 187 System.out.println( "Valid commands are upload <path> and save <path>" ); 188 } 189 } 190 } 191 catch ( java.io.IOException ioe ) 192 { 193 ioe.printStackTrace(); 194 } 195 catch ( javax.jms.JMSException jmse ) 196 { 197 jmse.printStackTrace(); 198 } 199 exit(); 201 } 202 203 207 public void onMessage( javax.jms.Message aMessage) 208 { 209 try 210 { 211 fileInPipe = (MantaFileMessage) aMessage; 213 214 217 String fileName = fileInPipe.getFileName(); 218 if(fileName!=null){ 219 System.out.println( "File '"+fileName+"' waiting to be saved" ); 220 System.out.println( "Type save <path> to save file" ); 221 } 222 223 224 } 225 catch (Throwable t) 226 { 227 t.printStackTrace(); 228 } 229 } 230 231 232 private void exit() 233 { 234 try 235 { 236 con.close(); 237 } 238 catch (javax.jms.JMSException jmse) 239 { 240 jmse.printStackTrace(); 241 } 242 243 System.exit(0); 244 } 245 246 251 252 public static void main(String argv[]) { 253 254 if (argv.length == 0) { 256 printHelp(); 257 waitForAnyKey(); 258 System.exit(1); 259 } 260 261 263 String username = null; 264 265 String qSender = null; 266 String qReceiver = null; 267 268 for (int i = 0; i < argv.length; i++) { 270 String arg = argv[i]; 271 272 if (!arg.startsWith("-")) { 274 System.err.println ("error: unexpected argument - "+arg); 275 printHelp(); 276 277 waitForAnyKey(); 278 System.exit(1); 279 } 280 else { 281 282 if (arg.equals("-u")) { 283 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 284 System.err.println("error: missing user name"); 285 System.exit(1); 286 } 287 username = argv[++i]; 288 continue; 289 } 290 291 if (arg.equals("-qr")) { 292 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 293 System.err.println("error: missing receive queue parameter"); 294 System.exit(1); 295 } 296 qReceiver = argv[++i]; 297 continue; 298 } 299 300 if (arg.equals("-qs")) { 301 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 302 System.err.println("error: missing send queue parameter"); 303 System.exit(1); 304 } 305 qSender = argv[++i]; 306 continue; 307 } 308 309 310 if (arg.equals("-h")) { 311 printHelp(); 312 System.exit(1); 313 } 314 } 315 } 316 317 if (username == null) { 319 System.err.println ("error: user name must be supplied"); 320 printHelp(); 321 System.exit(1); 322 } 323 324 if (qReceiver == null || qSender == null) { 325 System.err.println ("error: receive queue and send queue must be supplied"); 326 printHelp(); 327 System.exit(1); 328 } 329 330 FileTransfer transfer = new FileTransfer(); 332 transfer.transfer(username ,qReceiver, qSender); 333 334 } 335 336 private static void waitForAnyKey(){ 337 System.out.print("Press any key ..."); 338 try { 339 System.in.read(); 340 } catch (IOException e) { 341 e.printStackTrace(); 342 } 343 } 344 345 346 private static void printHelp() { 347 348 StringBuffer use = new StringBuffer (); 349 use.append("help: java Talk (options) ...\n\n"); 350 use.append("options:\n"); 351 use.append(" -qr queue Specify queue for receiving messages.\n"); 352 use.append(" -qs queue Specify queue for sending messages.\n"); 353 use.append(" -u user Specify a user name.\n"); 354 use.append(" -h This help screen.\n"); 355 System.err.println (use); 356 } 357 358 } | Popular Tags |