Websocket 推送通知给 Tomcat 上的多个 JSF 应用程序

huangapple go评论66阅读模式
英文:

Websocket push notification to several JSF applications on Tomcat

问题

我有两个在Tomcat上运行的JSF应用程序。

  • localhost:8080/app1/index.xhmtl
  • localhost:8080/app2/index.xhmtl

在两个索引文件中,都在表单部分放置了相同的f:websocket标签。

<h:form>

	...
	...
	...
	
    <f:websocket channel="test" onmessage="socketListener" />
    <h:outputScript>
        function socketListener(message, channel, event) {
            console.log(message);
        }
    </h:outputScript>

</h:form>

在两个托管bean中使用了相同的通道。

@ApplicationScoped
@Named
public class AppController implements Serializable
{
	@Inject
	@Push(channel = "test")
	private PushContext pushContext;

	public void sendMessage(String p_message)
	{
		pushContext.send(p_message);
	}
	
	...
	...
	...
	
}

但是,当我通过app1的JAVA方法pushContext.send发送通知时,app2的JS方法socketListener不起作用。

是否可能同时向多个JSF应用程序推送通知?

英文:

I have two JSF applications running on Tomcat.

  • localhost:8080/app1/index.xhmtl
  • localhost:8080/app2/index.xhmtl

In both index files the same f:websocket tag is placed in the form section.

&lt;h:form&gt;
	
	...
	...
	...
	
    &lt;f:websocket channel=&quot;test&quot; onmessage=&quot;socketListener&quot; /&gt;
    &lt;h:outputScript&gt;
        function socketListener(message, channel, event) {
            console.log(message);
        }
    &lt;/h:outputScript&gt;

&lt;/h:form&gt;

In both managed beans the same channel is used.

@ApplicationScoped
@Named
public class AppController implements Serializable
{
	@Inject
	@Push(channel = &quot;test&quot;)
	private PushContext pushContext;

	public void sendMessage(String p_message)
	{
		pushContext.send(p_message);
	}
	
	...
	...
	...
	
}

But when I send a notification via JAVA method pushContext.send of app1, the JS method socketListener of app2 does not work.

Is it possible to push notification to several JSF applications at once?

答案1

得分: 3

Sure, here's the translated content:

网络套接字默认为应用程序范围。两个应用程序都有自己的应用程序范围。两个应用程序根本不共享相同的应用程序范围。你看,应用程序范围是一个范围,嗯,与单个应用程序关联。这是因为一个应用程序就是一个应用程序。啊好吧,我想你现在明白了 Websocket 推送通知给 Tomcat 上的多个 JSF 应用程序

基本上,你想要跨应用程序通信。标准的Java EE API提供了JMS来实现这一点。但由于你正在使用一个裸骨的Tomcat Servlet容器,你的选择相当有限。你可以在Tomcat上安装JMS,但更好的方法是迁移到一个正常的Java EE服务器。Java EE版的Tomcat是TomEE。它已经预装了JSF、CDI、JMS等(基本上是Java EE的一切),无需通过WAR部署手动携带大量的JAR文件。

一旦在Tomcat中安装了JMS,或者迁移到了正常的Java EE服务器,那么你可以按照OmniFaces &lt;o:socket&gt;文档中的“集群设计提示”部分的说明&lt;f:websocket&gt;的教父)进行操作。

英文:

The web socket is by default application scoped. Both applications have their own application scope. Both applications don't at all share the same application scope. You see, the application scope is a scope which is, well, tied to a single application. That's because one application is one application. Ah well, I think you get it now Websocket 推送通知给 Tomcat 上的多个 JSF 应用程序

You basically want cross-application communication. Standard Java EE API offers JMS for this out the box. But as you're using a barebones Tomcat servlet container, your options are pretty limited. You could install JMS on Tomcat, but better is to migrate to a normal Java EE server. The Java EE variant of Tomcat is TomEE. It already ships JSF, CDI, JMS and more out the box (basically, everything from Java EE), without the need to manually carry around a lot of JAR files via the WAR deployment.

Once having JMS installed in Tomcat, or migrated to a normal Java EE server, then you can follow the instructions in "Cluster design hints" section of the documentation of OmniFaces &lt;o:socket&gt; (the godfather of the &lt;f:websocket&gt;).

huangapple
  • 本文由 发表于 2020年1月4日 00:45:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定