1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2013 Janos Szurovecz |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
7
|
|
|
* the Software without restriction, including without limitation the rights to |
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
10
|
|
|
* so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
13
|
|
|
* copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
* SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace predaddy\messagehandling\mf4php; |
25
|
|
|
|
26
|
|
|
use InvalidArgumentException; |
27
|
|
|
use mf4php\DefaultQueue; |
28
|
|
|
use mf4php\Message as Mf4phpMessage; |
29
|
|
|
use mf4php\MessageDispatcher; |
30
|
|
|
use mf4php\MessageListener; |
31
|
|
|
use mf4php\ObjectMessage; |
32
|
|
|
use precore\lang\NullPointerException; |
33
|
|
|
use precore\util\Preconditions; |
34
|
|
|
use predaddy\messagehandling\MessageCallback; |
35
|
|
|
use predaddy\messagehandling\SimpleMessageBus; |
36
|
|
|
use Serializable; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@link MessageBus} implementation which uses mf4php to forward messages. |
40
|
|
|
* |
41
|
|
|
* If you use a proper MessageDispatcher it is possible to |
42
|
|
|
* handle messages after the transaction has been committed. |
43
|
|
|
* |
44
|
|
|
* With an asynchronous {@link MessageDispatcher} implementation message |
45
|
|
|
* handling can be asynchronous. |
46
|
|
|
* |
47
|
|
|
* Do not register it to the given {@link MessageDispatcher}, |
48
|
|
|
* it is handled by default. |
49
|
|
|
* |
50
|
|
|
* Does not support {@link MessageCallback}! |
51
|
|
|
* |
52
|
|
|
* @author Janos Szurovecz <[email protected]> |
53
|
|
|
*/ |
54
|
|
|
class Mf4PhpMessageBus extends SimpleMessageBus implements MessageListener |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
private $dispatcher; |
57
|
|
|
private $queue; |
58
|
|
|
private $objectMessageFactories = []; |
59
|
|
|
private $defaultObjectMessageFactory; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Mf4PhpMessageBusBuilder $builder |
63
|
|
|
*/ |
64
|
|
|
public function __construct(Mf4PhpMessageBusBuilder $builder) |
65
|
|
|
{ |
66
|
|
|
parent::__construct($builder); |
67
|
|
|
$this->dispatcher = $builder->getDispatcher(); |
68
|
|
|
$this->queue = new DefaultQueue($builder->getIdentifier()); |
69
|
|
|
$this->defaultObjectMessageFactory = new DefaultObjectMessageFactory(); |
70
|
|
|
$this->dispatcher->addListener($this->queue, $this); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param MessageDispatcher $dispatcher the default value is due to PHP restrictions |
75
|
|
|
* @return Mf4PhpMessageBusBuilder |
76
|
|
|
* @throws NullPointerException if $dispatcher is null |
77
|
|
|
*/ |
78
|
|
|
public static function builder(MessageDispatcher $dispatcher = null) |
79
|
|
|
{ |
80
|
|
|
return new Mf4PhpMessageBusBuilder(Preconditions::checkNotNull($dispatcher)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Only full matching class names are used, you should not register |
85
|
|
|
* a factory for an abstract message class/interface! |
86
|
|
|
* |
87
|
|
|
* If there is no registered factory for a particular message class, |
88
|
|
|
* DefaultObjectMessageFactory will be used. |
89
|
|
|
* |
90
|
|
|
* @param ObjectMessageFactory $factory |
91
|
|
|
* @param $messageClass |
92
|
|
|
*/ |
93
|
|
|
public function registerObjectMessageFactory(ObjectMessageFactory $factory, $messageClass) |
94
|
|
|
{ |
95
|
|
|
$this->objectMessageFactories[$messageClass] = $factory; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Forward incoming message to handlers. |
100
|
|
|
* |
101
|
|
|
* @param Mf4phpMessage $message |
102
|
|
|
* @throws InvalidArgumentException |
103
|
|
|
*/ |
104
|
|
|
public function onMessage(Mf4phpMessage $message) |
105
|
|
|
{ |
106
|
|
|
Preconditions::checkArgument($message instanceof ObjectMessage, "Message must be an instance of ObjectMessage"); |
107
|
|
|
$object = $message->getObject(); |
108
|
|
|
if ($object instanceof MessageWrapper) { |
109
|
|
|
$object = $object->getMessage(); |
110
|
|
|
} |
111
|
|
|
parent::dispatch($object, self::emptyCallback()); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Finds the appropriate message factory for the given message. |
116
|
|
|
* |
117
|
|
|
* @param $message |
118
|
|
|
* @return ObjectMessageFactory |
119
|
|
|
*/ |
120
|
|
|
protected function findObjectMessageFactory($message) |
121
|
|
|
{ |
122
|
|
|
$messageClass = get_class($message); |
123
|
|
|
foreach ($this->objectMessageFactories as $class => $factory) { |
124
|
|
|
if ($class === $messageClass) { |
125
|
|
|
return $factory; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return $this->defaultObjectMessageFactory; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Send the message to the message queue. |
133
|
|
|
* |
134
|
|
|
* @param $message |
135
|
|
|
* @param MessageCallback $callback |
136
|
|
|
*/ |
137
|
|
|
protected function dispatch($message, MessageCallback $callback) |
138
|
|
|
{ |
139
|
|
|
$sendable = $message; |
140
|
|
|
if (!($message instanceof Serializable)) { |
141
|
|
|
$sendable = new MessageWrapper($message); |
142
|
|
|
} |
143
|
|
|
$mf4phpMessage = $this->findObjectMessageFactory($message)->createMessage($sendable); |
144
|
|
|
$this->dispatcher->send($this->queue, $mf4phpMessage); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|