-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
StatefulStomp.php
196 lines (176 loc) · 3.83 KB
/
StatefulStomp.php
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
<?php
/*
* This file is part of the Stomp package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Stomp;
use Stomp\States\IStateful;
use Stomp\States\Meta\SubscriptionList;
use Stomp\States\ProducerState;
use Stomp\States\StateSetter;
use Stomp\Transport\Frame;
use Stomp\Transport\Message;
/**
* Stateful Stomp Client
*
* This is a stateful implementation of a stomp client.
* This client will help you using stomp in a safe way by using the state machine pattern.
*
* @package Stomp
* @author Jens Radtke <swefl.oss@fin-sn.de>
*/
class StatefulStomp extends StateSetter implements IStateful
{
/**
* active state
*
* @var IStateful
*/
private $state;
/**
* @var Client
*/
private $client;
/**
* StatefulStomp constructor.
*
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
$this->state = new ProducerState($client, $this);
}
/**
* Acknowledge consumption of a message from a subscription
*
* @param Frame $frame
* @return void
*/
public function ack(Frame $frame)
{
$this->state->ack($frame);
}
/**
* Not acknowledge consumption of a message from a subscription
*
* @param Frame $frame
* @param bool $requeue requeue header not supported in all brokers
* @return void
*/
public function nack(Frame $frame, $requeue = null)
{
$this->state->nack($frame, $requeue);
}
/**
* Send a message.
*
* @param string $destination
* @param \Stomp\Transport\Message $message
* @return bool
*/
public function send($destination, Message $message)
{
return $this->state->send($destination, $message);
}
/**
* Begins an transaction.
*
* @return void
*/
public function begin()
{
$this->state->begin();
}
/**
* Commit current transaction.
*
* @return void
*/
public function commit()
{
$this->state->commit();
}
/**
* Abort current transaction.
*
* @return void
*/
public function abort()
{
$this->state->abort();
}
/**
* Subscribe to given destination.
*
* Returns the subscriptionId used for this.
*
* @param string $destination
* @param string $selector
* @param string $ack
* @param array $header
* @return int
*/
public function subscribe($destination, $selector = null, $ack = 'auto', array $header = [])
{
return $this->state->subscribe($destination, $selector, $ack, $header);
}
/**
* Unsubscribe from current or given destination.
*
* @param int $subscriptionId
* @return void
*/
public function unsubscribe($subscriptionId = null)
{
$this->state->unsubscribe($subscriptionId);
}
/**
* Returns as list of all active subscriptions.
*
* @return SubscriptionList
*/
public function getSubscriptions()
{
return $this->state->getSubscriptions();
}
/**
* Read a frame
*
* @return \Stomp\Transport\Frame|false
*/
public function read()
{
return $this->state->read();
}
/**
* Current State
*
* @return IStateful
*/
public function getState()
{
return $this->state;
}
/**
* Changes the current state.
*
* @param IStateful $state
* @return mixed
*/
protected function setState(IStateful $state)
{
$this->state = $state;
}
/**
* Returns the used client.
*
* @return Client
*/
public function getClient()
{
return $this->client;
}
}