-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrainMsgConsumer.java
More file actions
169 lines (141 loc) · 5.21 KB
/
TrainMsgConsumer.java
File metadata and controls
169 lines (141 loc) · 5.21 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
/*
* Copyright (c) 2001-2006 TIBCO Software Inc.
* All rights reserved.
* For more information, please contact:
* TIBCO Software Inc., Palo Alto, California, USA
*
* $Id: tibjmsAsyncMsgConsumer.java 21731 2006-05-01 21:41:34Z $
*
*/
/*
* This is a simple sample of a basic asynchronous
* tibjmsMsgConsumer.
*
* This sampe subscribes to specified destination and
* receives and prints all received messages.
*
* Notice that the specified destination should exist in your configuration
* or your topics/queues configuration file should allow
* creation of the specified destination.
*
* If this sample is used to receive messages published by
* tibjmsMsgProducer sample, it must be started prior
* to running the tibjmsMsgProducer sample.
*
* Usage: java tibjmsAsyncMsgConsumer [options]
*
* where options are:
*
* -server Server URL.
* If not specified this sample assumes a
* serverUrl of "tcp://localhost:7222"
*
* -user User name. Default is null.
* -password User password. Default is null.
* -topic Topic name. Default is "topic.sample"
* -queue Queue name. No default
*
*
*/
import java.io.IOException;
import javax.jms.*;
public class TrainMsgConsumer
implements ExceptionListener, MessageListener
{
public static final int QUEUE = 1;
public static final int TOPIC = 2;
/*-----------------------------------------------------------------------
* Parameters
*----------------------------------------------------------------------*/
private String serverUrl;
private String userName;
private String password;
private String name;
private int messageType;
/*-----------------------------------------------------------------------
* Variables
*----------------------------------------------------------------------*/
private Connection connection;
private Session session;
private MessageConsumer msgConsumer;
private Destination destination;
public TrainMsgConsumer(String serverUrl,String name,int messageType)
{
userName = "";
password = "";
this.serverUrl = serverUrl;
this.name = name;
this.messageType = messageType;
try
{
tibjmsUtilities.initSSLParams(serverUrl,new String[0]);
}
catch(JMSSecurityException e)
{
System.err.println("JMSSecurityException: "+e.getMessage()+", provider="+e.getErrorCode());
e.printStackTrace();
System.exit(0);
}
try
{
int c;
ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(serverUrl);
/* create the connection */
connection = factory.createConnection(userName,password);
/* create the session */
session = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
/* set the exception listener */
connection.setExceptionListener(this);
/* create the destination */
if(messageType == TrainMsgConsumer.TOPIC)
destination = session.createTopic(name);
else
destination = session.createQueue(name);
System.err.println("Subscribing to destination: "+name);
/* create the consumer */
msgConsumer = session.createConsumer(destination);
/* set the message listener */
msgConsumer.setMessageListener(this);
/* start the connection */
connection.start();
// Note: when message callback is used, the session
// creates the dispatcher thread which is not a daemon
// thread by default. Thus we can quit this method however
// the application will keep running. It is possible to
// specify that all session dispatchers are daemon threads.
}
catch(Exception e)
{
e.printStackTrace();
}
}
/*---------------------------------------------------------------------
* onException
*---------------------------------------------------------------------*/
public void onException(JMSException e)
{
/* print the connection exception status */
System.err.println("CONNECTION EXCEPTION: "+ e.getMessage());
}
/*---------------------------------------------------------------------
* onMessage
*---------------------------------------------------------------------*/
public void onMessage(Message msg)
{
try
{
TextMessage receivedMsg = (TextMessage) msg;
System.out.println();
System.err.println("=======================NEW MESSAGE=========================");
System.err.println("Received message from : " + receivedMsg.getJMSDestination());
System.err.println("Message: " + receivedMsg.getText());
System.out.println();
}
catch(Exception e)
{
System.err.println("Unexpected exception in the message callback!");
e.printStackTrace();
System.exit(-1);
}
}
}