1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.apache.commons.messenger.tool; |
11 | |
|
12 | |
import java.io.FileWriter; |
13 | |
import java.io.OutputStreamWriter; |
14 | |
import java.io.PrintWriter; |
15 | |
import java.io.Writer; |
16 | |
|
17 | |
import javax.jms.Destination; |
18 | |
import javax.jms.JMSException; |
19 | |
import javax.jms.Message; |
20 | |
import javax.jms.TextMessage; |
21 | |
|
22 | |
import org.apache.commons.messenger.Messenger; |
23 | |
import org.apache.commons.messenger.MessengerManager; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class Consumer { |
33 | |
|
34 | |
private Messenger messenger; |
35 | |
|
36 | |
public static void main(String[] args) { |
37 | |
try { |
38 | 0 | Consumer client = new Consumer(); |
39 | 0 | client.run( args ); |
40 | |
} |
41 | 0 | catch (JMSException e) { |
42 | 0 | System.out.println( "Caught: " + e ); |
43 | 0 | Exception linked = e.getLinkedException(); |
44 | 0 | if ( linked != null ) { |
45 | 0 | System.out.println( "Underlying exception: " + linked ); |
46 | 0 | linked.printStackTrace(); |
47 | |
} |
48 | |
else { |
49 | 0 | e.printStackTrace(); |
50 | |
} |
51 | |
} |
52 | 0 | catch (Exception e) { |
53 | 0 | System.out.println( "Caught: " + e ); |
54 | 0 | e.printStackTrace(); |
55 | 0 | } |
56 | 0 | } |
57 | |
|
58 | |
public void run(String[] args) throws Exception { |
59 | 0 | if ( args.length < 2 ) { |
60 | 0 | System.out.println( "Usage: Consumer messengerName destination [fileName]" ); |
61 | 0 | System.out.println( "If no fileName is provided then the current input stream is used" ); |
62 | 0 | return; |
63 | |
} |
64 | 0 | String name = args[0]; |
65 | 0 | String subject = args[1]; |
66 | 0 | messenger = MessengerManager.get( name ); |
67 | 0 | if ( messenger == null ) { |
68 | 0 | throw new JMSException( "No such messenger called: " + name ); |
69 | |
} |
70 | 0 | Destination destination = messenger.getDestination( subject ); |
71 | 0 | if ( destination == null ) { |
72 | 0 | throw new JMSException( "Could not find destination: " + subject ); |
73 | |
} |
74 | |
|
75 | 0 | Writer writer = ( args.length > 2 ) |
76 | |
? new FileWriter( args[2] ) |
77 | |
: new OutputStreamWriter( System.out ); |
78 | |
|
79 | 0 | Message message = messenger.receive( destination ); |
80 | |
|
81 | 0 | writeMessage( message, writer ); |
82 | |
|
83 | |
|
84 | 0 | messenger.close(); |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
protected void writeMessage(Message message, Writer out) throws Exception { |
89 | 0 | PrintWriter writer = new PrintWriter(out); |
90 | 0 | if ( message instanceof TextMessage ) { |
91 | 0 | TextMessage textMessage = (TextMessage) message; |
92 | 0 | writer.println( textMessage.getText() ); |
93 | |
} |
94 | 0 | writer.close(); |
95 | 0 | } |
96 | |
} |
97 | |
|
98 | |
|