简体   繁体   English

Java EE替代使用JMS发布订阅

[英]Java EE alternative to publish subscribe using JMS

I'm new to Java EE and have been looking for a common way to implement a publish/subscribe type model without having to use JMS. 我是Java EE的新手,一直在寻找一种实现发布/订阅类型模型的常用方法,而不必使用JMS。 I'm interested in creating a simple app that displays strings as they are push from the server to the client. 我有兴趣创建一个简单的应用程序,当它们从服务器推送到客户端时显示字符串。 I want to investigate was of doing this without polling to reduce unnecessary requests. 我想调查是在没有轮询的情况下这样做以减少不必要的请求。 The events would be varied quite a bit so I don't think polling over a set amount of time would be the best solution but the client should display the event immediately. 这些事件会有很大的不同,所以我认为在一定时间内轮询不是最佳解决方案,但客户应该立即显示事件。

I've read about different ways of doing this outside of Java EE such as HtML5 with sockets api. 我已经阅读了在Java EE之外执行此操作的不同方法,例如带有套接字api的HtML5。 But I want to know how to do this in Java EE, I'm assuming there is something very common that does this but I have not come across it yet. 但我想知道如何在Java EE中执行此操作,我假设有一些非常常见的事情,但我还没有遇到它。 Really I'm just looking for the technology name so that I can do further research on its implementation. 真的,我只是在寻找技术名称,以便我可以对其实施进行进一步的研究。

Maybe Hazelcast is worth for you to have a look at. 也许Hazelcast值得你去看看。 It offers an easy to use Distributed Topic feature for publish/subscribe messaging. 它为发布/订阅消息传递提供了易于使用的分布式主题功能。

A simple example from the docs: 文档中的一个简单示例:

import com.hazelcast.core.Topic;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.MessageListener;

public class Sample implements MessageListener {

    public static void main(String[] args) { 
        Sample sample = new Sample();
        Topic topic = Hazelcast.getTopic ("default");  
        topic.addMessageListener(sample);
        topic.publish ("my-message-object");
    }  

    public void onMessage(Object msg) {
        System.out.println("Message received = " + msg);
    } 
}

I am not an expert on this topic but since no one has answered I will try to explain what I know. 我不是这个主题的专家,但由于没有人回答我会尝试解释我所知道的。

First of all J2EE uses JMS spec as its fundamental publish/subcribe mechanism. 首先,J2EE使用JMS规范作为其基本的发布/订阅机制。 There are various JMS brokers out there. 那里有各种JMS经纪人。 The important point here is that some of these brokers are not specifically tied to any J2EE application server and can work stand alone. 这里重要的一点是,这些代理中的一些并不专门与任何J2​​EE应用程序服务器绑定,并且可以独立工作。 Check out Apache's ActiveMQ and it works well as a standalone JMS broker. 查看Apache的ActiveMQ,它可以作为独立的JMS代理使用。 It has bindings to many languages a well. 它很好地绑定了许多语言。 So you can freely compose a non J2EE architecture using a JMS broker. 因此,您可以使用JMS代理自由组合非J2EE体系结构。

Second, there are other message queue brokers that comply to other standards that can be used within a J2EE architecture as well. 其次,还有其他消息队列代理符合可以在J2EE体系结构中使用的其他标准。 DDS (Data Distribution Service) is an example. DDS(数据分发服务)就是一个例子。 It is an OMG standard and has bindings for Java and can be used in a J2EE architecture if desired. 它是一个OMG标准,具有Java绑定,如果需要,可以在J2EE体系结构中使用。

Third, Web Services standards define a WS-Notification Broker standard. 第三,Web服务标准定义了WS-Notification Broker标准。 Again this is not part of J2EE as far as I know but is supported by many SOA providers. 据我所知,这不是J2EE的一部分,但是得到了许多SOA提供商的支持。

So you have many alternatives that can be freely mixed in J2EE architecture. 因此,您有许多可以在J2EE体系结构中自由混合的替代方案。

I hope this helps. 我希望这有帮助。

I'll answer only the Java EE part, as I'm not familiar with the Apple technology you quoted. 我只回答Java EE部分,因为我不熟悉你引用的Apple技术。

It seems to me enterprise Java is not appropriate for this kind of task. 在我看来,企业Java不适合这种任务。 The main use case for a Java EE application is: "lots of users do lots of small, mostly independent tasks on a centralized application". Java EE应用程序的主要用例是:“许多用户在集中式应用程序上执行大量小型,大部分独立的任务”。 Java EE provides means for the central application to scale to any number of users. Java EE为中央应用程序提供了扩展到任意数量用户的方法。 And it's mostly users who initiate and steer the dialogue. 而且主要是用户发起和引导对话。

Your use case, however, requires the server to be the active part. 但是,您的用例要求服务器是活动部分。 You can, of course, run almost any kind of logic on a Java EE application server, asynchronous tasks, too, but it doesn't mean you should. 当然,您可以在Java EE应用程序服务器上运行几乎任何类型的逻辑,异步任务,但这并不意味着您应该这样做。

Redis有一个可能感兴趣的发布/订阅机制: http//redis.io/topics/pubsub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM