简体   繁体   English

如何在jms接收器上应用观察者模式

[英]How do I apply Observer Pattern with a jms receiver

I have a java application that receives messages (using RabbitMQ). 我有一个接收消息的Java应用程序(使用RabbitMQ)。 But I don't know how to apply the observer pattern to this. 但是我不知道如何将观察者模式应用于此。 The incoming messages contain a position String and an incident String. 传入消息包含位置字符串和入射字符串。

Application structure: 应用结构:

  • adapters 适配器

    • MovementAPI -> class to handle incoming messages from RabbitMQ MovementAPI->类,用于处理来自RabbitMQ的传入消息
  • domain

    • Controller -> concrete Observer? 控制器->具体的观察者?
    • Message 信息
    • (i) Observer (i)观察员
    • (i) Subject (i)主题

I hope this is enough info. 我希望这是足够的信息。

The basic of of observer pattern says you will have a subject which will have broadcast to which registered objects (who are observers) will be notified 观察者模式的基本原理是,您将有一个主题,该主题将广播通知哪些注册对象(谁是观察者)

-- make a following interface which will be inplemented by your observers. -制作一个以下界面,您的观察者将对其进行补充。

public interface Observer {
void update(String incident, String position);
}

make another interface which will be implemented by the Subject which has to broadcast the message 制作另一个接口,该接口将由必须广播消息的主题实现

 public interface Subject {

    void registerObserver(Observer observer);

    void removeObserver(Observer observer);

    void notifyObservers();
}

lets have your class modified who's object will notify the other observer objects 让我们修改您的类,谁的对象将通知其他观察者对象

public class YourBroadCastObjectData implements Subject {

private ArrayList<Observer> observers;
private String incident;
private String position;

public YourBroadCastObjectData() {
    // TODO Auto-generated constructor stub
    observers = new ArrayList<>();
}

@Override
public void notifyObservers() {
    // TODO Auto-generated method stub
    for (int i = 0; i < observers.size(); i++) {
        Observer observer = observers.get(i);
        observer.update(this.incident, this.position);
    }
}

@Override
public void registerObserver(Observer observer) {
    // TODO Auto-generated method stub
    observers.add(observer);

}

@Override
public void removeObserver(Observer observer) {
    // TODO Auto-generated method stub
    int i = observers.lastIndexOf(observer);
    if (i >= 0) {
        observers.remove(i);
    }

}

public void dataChanged() {
    notifyObservers();
}

public void setData(String incident, String position) {
    this.incident= incident;
    this.position= position;
    dataChanged();
}

public float getIncident() {
    return incident;
}

public float getPosition() {
    return position;
}



}

As far as I know you will have a asynchronous thread to get the data from the RabbitMQ Service to your application, in that thread have your subject notify all the objects. 据我所知,您将有一个异步线程将数据从RabbitMQ服务获取到您的应用程序,在该线程中,您的主题将通知所有对象。

you will have to implement the observer interface to your class who's object you want to act as observer. 您将必须为要充当观察者的对象的类实现观察者接口。 check some example online to see how observer pattern works. 在线查看一些示例以了解观察者模式如何工作。

Java provides in built support for observer pattern but it is concrete class which you have to inherit. Java为观察者模式提供了内置支持,但它是您必须继承的具体类。

TO provide more flexible design you would better make your own pattern according to your need. 为了提供更灵活的设计,您最好根据需要制作自己的图案。

above code will help you push the data (Example.. incident and position), but you can also make pull pattern 上面的代码将帮助您推送数据(示例..事件和位置),但您也可以创建拉模式

As you would be implementing RabbitMQ you must have some data which is keep updating itself on server and your consumer code is hooked up to the producer of RabbitMQ service on server. 在实施RabbitMQ时,您必须拥有一些数据,这些数据会在服务器上不断更新,并且您的使用者代码已挂接到服务器上RabbitMQ服务的生产者。

once you get the data from the consumer code you can have any class as subject (YorBroadCastObjectData) to implement SUbject and this object will send the data to observer objects. 从使用者代码获取数据后,您可以将任何类作为主题(YorBroadCastObjectData)来实现SUbject,此对象会将数据发送给观察者对象。 while making observer object give a reference to your subject's object that your observer object can register it self as a observer to the subject's object 在使观察者对象引用对象对象的同时,观察者对象可以将其自身注册为对象对象的观察者

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

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