Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC,Comed:没有@Listener的广播_Java_Spring_Spring Mvc_Cometd_Reverse Ajax - Fatal编程技术网

Java Spring MVC,Comed:没有@Listener的广播

Java Spring MVC,Comed:没有@Listener的广播,java,spring,spring-mvc,cometd,reverse-ajax,Java,Spring,Spring Mvc,Cometd,Reverse Ajax,我正在开发一个SpringMVC应用程序,多亏了上面的用户,我们已经有了一个Comet聊天功能。我们在应用程序中的另一个功能是通知,但我们希望在通知发生时立即集成实时通知,有点像Facebook 基本上,这个想法是,每当创建一个新的通知时,它都会保存在数据库中,并且它的信息必须从后端传递给每个用户在唯一通道上登录的用户的通知 我想知道这种方法是否有效,因为我需要做一些工作才能将通知路由到聊天类。请注意,我也没有ChatServiceImpl类的接口。可以吗?说够了,下面是代码: ChatServ

我正在开发一个SpringMVC应用程序,多亏了上面的用户,我们已经有了一个Comet聊天功能。我们在应用程序中的另一个功能是通知,但我们希望在通知发生时立即集成实时通知,有点像Facebook

基本上,这个想法是,每当创建一个新的通知时,它都会保存在数据库中,并且它的信息必须从后端传递给每个用户在唯一通道上登录的用户的通知

我想知道这种方法是否有效,因为我需要做一些工作才能将通知路由到聊天类。请注意,我也没有ChatServiceImpl类的接口。可以吗?说够了,下面是代码:

ChatServiceImpl:

@Named
@Singleton
@Service
public class ChatServiceImpl {
    @Inject
    private BayeuxServer bayeux;

    @Session
    private ServerSession serverSession;


    public void sendNotification(Notification notification,int id
// And then I send notification here like below, by extracting information from the notification object.

 ServerChannel serverChannel = bayeux.createChannelIfAbsent("/person/notification/" + id).getReference();
        serverChannel.setPersistent(true);
        serverChannel.publish(serverSession, output);
        }
    }
上面的类没有接口,所以我计划使用如下方法:

@Service
@Transactional
public class GroupCanvasServiceImpl implements GroupCanvasService{
    private ChatServiceImpl chatService;

   public void someMethod(){
   chatService.sendNotification(notification, id);
}
}
Bayeuxin初始化器:

@Component
public class BayeuxInitializer implements DestructionAwareBeanPostProcessor, ServletContextAware
{
    private BayeuxServer bayeuxServer;
    private ServerAnnotationProcessor processor;

    @Inject
    private void setBayeuxServer(BayeuxServer bayeuxServer)
    {
        this.bayeuxServer = bayeuxServer;
    }

    @PostConstruct
    private void init()
    {

        this.processor = new ServerAnnotationProcessor(bayeuxServer);
    }

    @PreDestroy
    private void destroy()
    {
        System.out.println("Bayeux in PreDestroy");
    }

    public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
    {
        processor.processDependencies(bean);
        processor.processConfigurations(bean);
        processor.processCallbacks(bean);
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
    {
        return bean;
    }

    public void postProcessBeforeDestruction(Object bean, String name) throws BeansException
    {
        processor.deprocessCallbacks(bean);
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public BayeuxServer bayeuxServer()
    {
        return new BayeuxServerImpl();
    }

    public void setServletContext(ServletContext servletContext)
    {
        servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer);
    }
}

请让我知道这个方法是否可行。非常感谢

注释
@Listener
用于处理从远程客户端接收的消息的方法

如果只需要发送服务器到客户端的消息,则不需要严格地使用
@Listener
注释任何方法:检索要发布到的
服务器频道
,并使用它发布消息就足够了

在您的特定情况下,似乎不需要在频道上为多个订阅者广播消息,但您只需要将消息发送到由
id
参数标识的特定客户端

如果是这样的话,那么以这种方式使用点对点消息可能更好:

public void sendNotification(通知通知,int-id)
{
ServerSession remoteClient=retrieveSessionFromId(id);
deliver(serverSession,“/person/notification”,notification);
}
此解决方案的优点是创建的频道要少得多(您不需要每个
id
创建一个频道)

更好的是,您可以将
/person/notification
频道(广播频道)替换为服务频道,如
/service/notification
。 通过这种方式,很明显,用于传递通知的信道用于对等通信(因为服务信道不能用于广播消息)


retrieveSessionFromId()
方法是用户登录时必须映射的方法,例如,请参阅有关的文档。

默认的家伙已经使用server.publish工作了,但是使用.deliver更有意义。您有通过Spring Security使用retrieveSessionFromId()的代码吗?我会调查此事,如果有任何问题,我会发帖提问。非常感谢。:-)你能检查一下这个问题吗:。非常感谢。:-)