Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 949b630850d2ca4598005f83a6236e98e5e9c4bd (plain) (blame)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.internal.jms;

import org.eclipse.net4j.internal.jms.bundle.OM;
import org.eclipse.net4j.internal.jms.protocol.JMSAcknowledgeRequest;
import org.eclipse.net4j.internal.jms.protocol.JMSClientMessageRequest;
import org.eclipse.net4j.internal.jms.protocol.JMSCommitRequest;
import org.eclipse.net4j.internal.jms.protocol.JMSRecoverRequest;
import org.eclipse.net4j.internal.jms.protocol.JMSRegisterConsumerRequest;
import org.eclipse.net4j.internal.jms.protocol.JMSRollbackRequest;
import org.eclipse.net4j.internal.jms.util.DestinationUtil;
import org.eclipse.net4j.internal.jms.util.MessageUtil;
import org.eclipse.net4j.util.lifecycle.QueueWorker;

import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.IllegalStateException;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TemporaryQueue;
import javax.jms.TemporaryTopic;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;
import javax.transaction.TransactionRolledbackException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SessionImpl extends QueueWorker<MessageConsumerImpl> implements Session
{
  private ConnectionImpl connection;

  private int id;

  private boolean transacted;

  private int acknowledgeMode;

  private Map<Long, MessageConsumerImpl> consumers = new HashMap<Long, MessageConsumerImpl>();

  /**
   * Outgoing transacted messages
   */
  private List<MessageImpl> messages = new ArrayList<MessageImpl>();

  private Set<MessageProducerImpl> producers = new HashSet<MessageProducerImpl>();

  public SessionImpl(ConnectionImpl connection, int id, boolean transacted, int acknowledgeMode) throws JMSException
  {
    this.connection = connection;
    this.id = id;
    this.transacted = transacted;
    this.acknowledgeMode = acknowledgeMode;

    try
    {
      activate();
    }
    catch (Exception ex)
    {
      throw new JMSException(ex.getMessage());
    }
  }

  public ConnectionImpl getConnection()
  {
    return connection;
  }

  public int getID()
  {
    return id;
  }

  public boolean getTransacted()
  {
    return transacted;
  }

  public int getAcknowledgeMode()
  {
    return acknowledgeMode;
  }

  public MessageListener getMessageListener()
  {
    return null;
  }

  public void setMessageListener(MessageListener listener)
  {
    throw new UnsupportedOperationException();
  }

  public MessageProducer createProducer(Destination destination) throws JMSException
  {
    DestinationImpl dest = DestinationUtil.convert(destination);
    MessageProducerImpl producer = new MessageProducerImpl(this, dest);
    producers.add(producer);
    return producer;
  }

  public MessageConsumer createConsumer(Destination destination) throws JMSException
  {
    return createConsumer(destination, null);
  }

  public MessageConsumer createConsumer(Destination destination, String messageSelector) throws JMSException
  {
    return createConsumer(destination, null, false);
  }

  public MessageConsumer createConsumer(Destination destination, String messageSelector, boolean noLocal)
      throws JMSException
  {
    DestinationImpl dest = DestinationUtil.convert(destination);
    long consumerID = registerConsumer(dest, messageSelector, noLocal, false);
    MessageConsumerImpl consumer = new MessageConsumerImpl(this, consumerID, dest, messageSelector);
    consumers.put(consumerID, consumer);
    return consumer;
  }

  public Queue createQueue(String queueName)
  {
    return new QueueImpl(queueName);
  }

  public TemporaryQueue createTemporaryQueue()
  {
    return new TemporaryQueueImpl();
  }

  public QueueBrowser createBrowser(Queue queue)
  {
    return new QueueBrowserImpl(queue);
  }

  public QueueBrowser createBrowser(Queue queue, String messageSelector)
  {
    return new QueueBrowserImpl(queue, messageSelector);
  }

  public Topic createTopic(String topicName)
  {
    return new TopicImpl(topicName);
  }

  public TemporaryTopic createTemporaryTopic()
  {
    return new TemporaryTopicImpl();
  }

  public TopicSubscriber createDurableSubscriber(Topic topic, String name) throws JMSException
  {
    return createDurableSubscriber(topic, name, null, false);
  }

  public TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal)
      throws JMSException
  {
    TopicImpl dest = (TopicImpl)DestinationUtil.convert(topic);
    long consumerID = registerConsumer(dest, messageSelector, noLocal, true);
    TopicSubscriberImpl subscriber = new TopicSubscriberImpl(this, consumerID, dest, name, messageSelector, noLocal);
    consumers.put(consumerID, subscriber);
    return subscriber;
  }

  public void unsubscribe(String name)
  {
    throw new NotYetImplementedException();
  }

  public Message createMessage()
  {
    return new MessageImpl();
  }

  public StreamMessage createStreamMessage()
  {
    return new StreamMessageImpl();
  }

  public BytesMessage createBytesMessage()
  {
    return new BytesMessageImpl();
  }

  public MapMessage createMapMessage()
  {
    return new MapMessageImpl();
  }

  public ObjectMessage createObjectMessage()
  {
    return createObjectMessage(null);
  }

  public ObjectMessage createObjectMessage(Serializable object)
  {
    return new ObjectMessageImpl(object);
  }

  public TextMessage createTextMessage()
  {
    return createTextMessage(null);
  }

  public TextMessage createTextMessage(String text)
  {
    return new TextMessageImpl(text);
  }

  public void recover() throws JMSException
  {
    ensureNotTransacted();
    try
    {
      stop();
      new JMSRecoverRequest(connection.getChannel(), id).send();
      start();
    }
    catch (Exception ex)
    {
      OM.LOG.error(ex);
      close();
    }
  }

  public void commit() throws JMSException
  {
    ensureTransacted();
    synchronized (messages)
    {
      try
      {
        String[] messageIDs = new JMSCommitRequest(connection.getChannel(), id, messages).send();
        if (messageIDs == null)
        {
          throw new TransactionRolledbackException("Transaction rolled back by JMS server");
        }

        for (int i = 0; i < messageIDs.length; i++)
        {
          messages.get(i).setJMSMessageID(messageIDs[i]);
        }

        messages.clear();
      }
      catch (JMSException ex)
      {
        throw ex;
      }
      catch (RuntimeException ex)
      {
        throw ex;
      }
      catch (Exception ex)
      {
        throw new JMSException(ex.getMessage());
      }
    }
  }

  public void rollback() throws JMSException
  {
    ensureTransacted();
    synchronized (messages)
    {
      try
      {
        if (!new JMSRollbackRequest(connection.getChannel(), id).send())
        {
          throw new JMSException("JMS server failed to rolled back transaction");
        }

        messages.clear();
      }
      catch (JMSException ex)
      {
        throw ex;
      }
      catch (RuntimeException ex)
      {
        throw ex;
      }
      catch (Exception ex)
      {
        throw new JMSException(ex.getMessage());
      }
    }
  }

  public void close()
  {
    deactivate();
  }

  public void run()
  {
    throw new UnsupportedOperationException();
  }

  public long registerConsumer(DestinationImpl destination, String messageSelector, boolean noLocal, boolean durable)
      throws JMSException
  {
    try
    {
      return new JMSRegisterConsumerRequest(connection.getChannel(), id, destination, messageSelector, noLocal, durable)
          .send();
    }
    catch (Exception ex)
    {
      throw new JMSException(ex.getMessage());
    }
  }

  public void sendMessage(Message message) throws JMSException
  {
    if (getTransacted())
    {
      synchronized (messages)
      {
        if (message instanceof MessageImpl)
        {
          messages.add(MessageUtil.copy(message));
        }
        else
        {
          messages.add(MessageUtil.convert(message));
        }
      }
    }
    else
    {
      try
      {
        MessageImpl impl = MessageUtil.convert(message);
        JMSClientMessageRequest request = new JMSClientMessageRequest(connection.getChannel(), impl);
        String messageID = request.send(connection.getSendTimeout());
        if (messageID == null)
        {
          throw new JMSException("Message not accepted by JMS server");
        }

        message.setJMSMessageID(messageID);
      }
      catch (JMSException ex)
      {
        throw ex;
      }
      catch (RuntimeException ex)
      {
        throw ex;
      }
      catch (Exception ex)
      {
        throw new JMSException(ex.getMessage());
      }
    }
  }

  public boolean acknowledgeMessages(MessageConsumerImpl consumer)
  {
    try
    {
      new JMSAcknowledgeRequest(connection.getChannel(), id).send();
      return true;
    }
    catch (Exception ex)
    {
      OM.LOG.error(ex);
      return true;
    }
  }

  public void handleServerMessage(long consumerID, MessageImpl message)
  {
    message.setReceivingSession(this);
    MessageConsumerImpl consumer = consumers.get(consumerID);
    if (consumer == null)
    {
      OM.LOG.warn("Consumer " + consumerID + " not found. Discarding message.");
      return;
    }

    consumer.handleServerMessage(message);
  }

  @Override
  protected String getThreadName()
  {
    return "jms-session";
  }

  @Override
  protected void work(WorkContext context, MessageConsumerImpl consumer)
  {
    consumer.dispatchMessage();
  }

  private void ensureTransacted() throws IllegalStateException
  {
    if (!transacted)
    {
      throw new IllegalStateException("Session " + id + " not transacted");
    }
  }

  private void ensureNotTransacted() throws IllegalStateException
  {
    if (transacted)
    {
      throw new IllegalStateException("Session " + id + " transacted");
    }
  }

  private void start()
  {
  }

  private void stop()
  {
  }
}

Back to the top