Skip to content

Commit 107f6fc

Browse files
author
Igor Polevoy
committed
#258 CommandListener needs to get injected dependencies
1 parent 3c6fbce commit 107f6fc

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

javalite-async/src/main/java/org/javalite/async/Async.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ private void configureListeners(Injector injector, List<QueueConfig> queueConfig
197197
for (int i = 0; i < queueConfig.getListenerCount(); i++) {
198198
CommandListener listener = (CommandListener) queueConfig.getCommandListenerClass().newInstance();
199199
listener.setInjector(injector);
200+
if(injector != null){
201+
injector.injectMembers(listener);
202+
}
200203
Session session = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
201204
MessageConsumer consumer = session.createConsumer(queue);
202205
consumer.setMessageListener(listener);

javalite-async/src/test/java/org/javalite/async/AsyncSpec.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,19 @@ public void shouldStartStopBroker() throws IOException, InterruptedException {
158158
async.stop();
159159
a(HelloCommand.counter()).shouldBeEqual(20);
160160
}
161+
162+
@Test
163+
public void shouldInjectDependencyIntoCommandListener() throws InterruptedException {
164+
165+
Injector injector = Guice.createInjector(new GreetingModule());
166+
Async async = new Async(filePath, false, injector, new QueueConfig(QUEUE_NAME, HelloCommandListener.class, 1));
167+
async.start();
168+
169+
async.send(QUEUE_NAME, new HelloCommand("Hi, there"));
170+
171+
//SEE ASSERTION INSIDE HelloCommandListener.
172+
173+
Thread.sleep(1000);
174+
async.stop();
175+
}
161176
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.javalite.async;
2+
3+
import com.google.inject.Inject;
4+
import org.javalite.async.services.GreetingService;
5+
6+
/**
7+
* @author Igor Polevoy on 2/2/16.
8+
*/
9+
public class HelloCommandListener extends CommandListener{
10+
11+
@Inject
12+
private GreetingService greetingService;
13+
14+
@Override
15+
public <T extends Command> void onCommand(T command) {
16+
assert greetingService != null;
17+
super.onCommand(command);
18+
}
19+
}

0 commit comments

Comments
 (0)