简体   繁体   English

如何从Java EE应用程序提供套接字?

[英]How to serve a socket from a Java EE application?

We would like to serve some client requests within our Java EE application server (JBoss 4.2.3). 我们希望在Java EE应用服务器(JBoss 4.2.3)中提供一些客户端请求。 I read that the Java EE spec doesn't allow opening a socket from an enterprise bean . 我读到Java EE规范不允许从企业bean打开套接字 But the spec suggests no alternatives. 但该规范表明没有其他选择。

Specifically, enterprise beans should not: 具体来说,企业bean不应该:

  • listen on, accept connections on, or multicast from a network socket 监听,接受网络套接字上的连接或多播

So the question is: What can I do to serve some binary tcp based protocols (not http) from within an application server? 所以问题是:我可以做什么来从应用服务器中提供一些基于二进制 tcp的协议(而不是http)?

Here someone suggests to implement a resource adapter . 这里有人建议实现资源适配器 Is this the way you have to go or are there other (easier) solutions? 这是你必须去的方式还是其他(更容易)的解决方案?

You're right, since you can declare transactions for every thing in Java EE they must be supported from all components you want to connect. 您是对的,因为您可以为Java EE中的每个事物声明事务,所以必须从您要连接的所有组件支持它们。 Files if any should be stored in a database. 文件(如果有)应存储在数据库中。 Everything should be controlled by the container, because it the only way to have a scaling application using Java EE. 一切都应该由容器控制,因为它是使用Java EE实现扩展应用程序的唯一方法。

A few options: 一些选择:

Neither should you access files :( 你也不应该访问文件:(

This is in the spec because EJBs have to be: 这是在规范中,因为EJB必须是:

  • distributable (you don't know beforehand on what server/instance your EJB will be deployed distributable(您事先不知道将部署EJB的服务器/实例)
  • the container is to be able to manage "everything" so you must not spawn your own threads 容器是能够管理“一切”所以你不能产生自己的线程

That in mind nothing will halt you from starting a server socket in your app (the best place would probably be in a servlet) but you should take care about how the serversocket is closed when your app goes down... 记住,没有什么会阻止你在你的应用程序中启动服务器套接字(最好的位置可能是在servlet中)但你应该注意当你的应用程序关闭时serversocket如何关闭...

Right now I implement a workaround: 现在我实现了一个解决方法:

alt text http://yuml.me/7f82bd5c alt text http://yuml.me/7f82bd5c

I use a standalone java application that accepts tcp calls from the client and forwards them as JNDI calls to the application server. 我使用一个独立的java应用程序,它接受来自客户端的tcp调用,并将它们作为JNDI调用转发给应用程序服务器。

I have used somewhat similar solution as per my need in Spring MVC. 根据我在Spring MVC中的需要,我使用了一些类似的解决方案。 Might be this can help someone here around. 可能这可以帮助这里的人。

Start a Socket Port on server start up. 在服务器启动时启动套接字端口。 I used @scheduler annotation whereas you can use listener based solution as well. 我使用了@scheduler注释,你也可以使用基于监听器的解决方案。 You can also implement ApplicationContextAware listener and can access other application beans from it. 您还可以实现ApplicationContextAware侦听器,并可以从中访问其他应用程序bean。

 @Scheduled(fixedDelay = 1000 * 60 * 60 * 24 * 365) public void startListenerPort() { ServerSocket socket = new ServerSocket(9999); // do some stuff here } 

Just make sure that you have allowed TCP traffic on the port that you have assigned to the Socket (Firewall Settings). 只需确保您已在分配给套接字的端口(防火墙设置)上允许TCP流量。

In this way, you can have TCP traffic on port 9999, where as your app server will continue to run on different port as normal. 通过这种方式,您可以在端口9999上拥有TCP流量,因为您的应用服务器将继续在不同的端口上正常运行。

Although not strictly a purely TCP connection, but you may be able to achieve what you need with a @ServerEndpoint annotation to create a WebSocket from the Java EE7 spec. 虽然不是严格意义上的纯TCP连接,但您可以通过@ServerEndpoint注释来实现所需,从Java EE7规范创建WebSocket。

Although this DOES use HTTP, it will function a little bit like a binary interface when your @OnMessage method and a ByteBuffer (or byte[] ) as the argument. 虽然这个DOES使用HTTP,但当你的@OnMessage方法和ByteBuffer (或byte[] )作为参数时,它的功能有点像二进制接口。

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

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