3

I am developing a web app and trying to integrate a chat (for now). I am using Spring and Atmosphere to do this. I managed to get the chat working, but now I seem to have run into a problem. I am using the @ManagedService in a separate servlet (similar to the atmosphere-chat-multiroom example) and now I need to access to a bean that is inside the spring application. For the rest of the servlets that I am using, this works flawlessly, but I am at a dead end when it comes to doind this in Atmosphere ManagedService, the service is always returned as null.

Does anyone have any clue as to what I can do?

@ManagedService(path = "{room: [a-zA-Z][a-zA-Z_0-9]*}")
@Singleton
public class ChatRoom {
private final Logger logger = LoggerFactory.getLogger(ChatRoom.class);

private final ConcurrentHashMap<String, String> users = new ConcurrentHashMap<String, String>();
private String chatroomName;
private String mappedPath;
private BroadcasterFactory factory;
@Autowired
IFriendsServices friendServices;

@Message(encoders = { JacksonEncoder.class }, decoders = { UserDecoder.class })
public void onPrivateMessage(ChatUserMessageDTO user) throws IOException {
    String userUUID = users.get(user.getUser());
    friendServices.createChatMessage(user.getUser(), user.getSource(), user.getMessage());
    if (userUUID != null) {
        AtmosphereResource r = AtmosphereResourceFactory.getDefault().find(userUUID);
        if (r != null) {
            ChatProtocolMessageDTO m = new ChatProtocolMessageDTO(user.getSource(), user.getMessage(),
                    users.keySet(), factory.lookupAll());
            factory.lookup(mappedPath).broadcast(m, r);
        }
    }
}
  • 1
    You could show us your code. – Uooo Nov 05 '13 at 11:59
  • What are the annotations of rest of the servlets and the ManagedService? – cy3er Nov 05 '13 at 12:00
  • 1
    the atmosphere managedservice has only the managedService and Singleton. The other ones usually only have Controller – Tiago Ferreira Nov 05 '13 at 12:12
  • 1
    added some code. The friendServices is returned as null – Tiago Ferreira Nov 05 '13 at 12:16
  • Spring won't inject dependency into classes that are not spring components. Annotate your ManagedService with @Service too. – cy3er Nov 05 '13 at 12:18
  • @cy3er, unfortunetly I already tried that. – Tiago Ferreira Nov 05 '13 at 12:29
  • Then worst case you can implement an ApplicationContextAware class with a static getContext method and get the bean directly from the context. – cy3er Nov 05 '13 at 13:11
  • Delegate the @ManagedService creation to Spring, as described https://github.com/Atmosphere/atmosphere/wiki/Configuring-Atmosphere's-Classes-Creation-and-Injection – jfarcand Nov 05 '13 at 13:37
  • @jfarcand, i have tried that also. still not working with the autowire. – Tiago Ferreira Nov 05 '13 at 15:07
  • 1
    Annotate your class with @Configurable and follow the instructions here: http://www.olivergierke.de/2009/05/using-springs-configurable-in-three-easy-steps/ – Joe Mar 13 '14 at 18:56
  • You are going to have to wire the IFriendsService yourself. If you create a Singleton class that ApplicationContext Aware, you can get any Spring configured bean (using a lazy initialization approach). – user3360944 Mar 13 '14 at 19:35

1 Answers1

0

hello i find vary interesting thing when i was digging in spring . for non spring managed classes follow following 2 steps to access properties using autowire

public class ChatRoom extends SpringBeanAutowiringSupport{ 

and then use @Autowire 
Pankaj Sharma
  • 1,833
  • 1
  • 17
  • 22