Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 87e451f456e1bd5f92b4c177b4602738ec947d70 (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
/**
 * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others.
 * 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.jms.internal.server;

import org.eclipse.net4j.internal.jms.DestinationImpl;
import org.eclipse.net4j.internal.jms.MessageImpl;
import org.eclipse.net4j.jms.internal.server.bundle.OM;
import org.eclipse.net4j.jms.internal.server.messages.Messages;
import org.eclipse.net4j.jms.server.ISession;
import org.eclipse.net4j.jms.server.IStore;
import org.eclipse.net4j.jms.server.IStoreTransaction;
import org.eclipse.net4j.util.lifecycle.Lifecycle;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author Eike Stepper
 */
public class ServerSession extends Lifecycle implements ISession
{
  private ServerConnection connection;

  private int id;

  private ConcurrentMap<Long, ServerConsumer> consumers = new ConcurrentHashMap<Long, ServerConsumer>();

  public ServerSession(ServerConnection connection, int id)
  {
    this.connection = connection;
    this.id = id;
  }

  public ServerConnection getConnection()
  {
    return connection;
  }

  public int getID()
  {
    return id;
  }

  public long registerConsumer(DestinationImpl dest, String messageSelector, boolean noLocal, boolean durable)
  {
    Server server = connection.getServer();
    String name = dest.getName();
    ServerDestination destination = server.getDestination(name);
    if (destination == null)
    {
      OM.LOG.error(MessageFormat.format(Messages.getString("ServerSession_0"), name)); //$NON-NLS-1$
      return -1;
    }

    ServerConsumer consumer = server.createConsumer(destination, messageSelector, noLocal, durable);
    consumer.setSession(this);
    consumers.put(consumer.getID(), consumer);
    destination.addConsumer(consumer);
    return consumer.getID();
  }

  public void handleAcknowledge()
  {
    IStore store = connection.getServer().getStore();
    IStoreTransaction transaction = store.startTransaction();
    handleAcknowledgeInTransaction(transaction);
    store.commitTransaction(transaction);
  }

  public void handleAcknowledgeInTransaction(IStoreTransaction transaction)
  {
    for (ServerConsumer consumer : consumers.values())
    {
      consumer.handleAcknowledge(transaction);
    }
  }

  public void handleRecover()
  {
    IStore store = connection.getServer().getStore();
    IStoreTransaction transaction = store.startTransaction();
    Collection<ServerConsumer> values = consumers.values();
    for (ServerConsumer consumer : values)
    {
      consumer.handleRecover(transaction);
    }

    store.commitTransaction(transaction);
  }

  public String[] handleCommit(MessageImpl[] messages)
  {
    Server server = connection.getServer();
    IStore store = server.getStore();

    IStoreTransaction transaction = store.startTransaction();
    handleAcknowledgeInTransaction(transaction);
    String[] messageIDs = server.handleClientMessagesInTransaction(transaction, messages);
    store.commitTransaction(transaction);

    return messageIDs;
  }
}

Back to the top