0

我在Java中有这种情况:

在称为 I 的内部模块上。一个大模块 Z 通过 DBus 从 I 接收消息。这个大模块 Z 与外部服务器 E 通信(对于 Dbus 接收到的每条消息)。

通过 DBus 架构,每完成一个请求,在 Z 模块中都会创建一个新的线程来处理它。

与 E 沟通的最佳方法是什么?

a) Make a class with a method SendToExternalServer(). this method will be called every time that have a new DBus message. 
  i) The method has only one HttpUrlConnection.
  ii) The method create a differente HttpUrlConnection instance every time is called.

b) Same situation on a) but now the method SendToExternalServer() is static
  i) (same situations)
  ii) (same situations)

c) Same situation on a) but the class extends the interface Runnable
  i) (same situation)
  ii) (same situation)


d) OTHER ?

提前感谢若昂

4

1 回答 1

0

我对 dbus 一无所知,所以对此持保留态度。

不会选择选项 B,仅仅是因为单元测试。使用选项 A(或 C),您可以切换到“MockSendToExternalServer”,它只是记录或记录正在“发送”的消息,并且单元测试将查看这些消息。

我强烈倾向于选项 C,因为这样您就可以使用 java.util.concurrent ConcurrencyUtilities,例如 ExecutorService。对于您的第一遍实现,请使用简单的单线程队列,(例如Executors.newSingleThreadedExecutor)但是,如果/当您需要更多线程时,您可以添加它们,例如Executors.newFixedThreadPool()

于 2012-04-18T16:09:24.413 回答