应用程序一直尝试重新启动消息服务,如何只运行一次消息服务?

时间:2018-08-31 17:41:58

标签: spring-boot executorservice

我有一个应用程序,该应用程序运行带有2个可运行程序的执行程序服务,该程序可启动过程以从rfid阅读器接收消息。阅读器从标签上获取标签后,会从消息侦听器接收一条消息,其中包含标签信息。我收到标签信息,但在运行bc时收到错误消息,它一直尝试重新启动messagelistenerservice。尝试仅添加需要查看的代码。如何只启动一次messagelistener服务,以免出现此错误。谢谢!

这是错误:

seaborn.heatmap

这是供读者使用的@Component:

java.io.IOException: Could not listen on port 3500. Is that port in use?
at com.alien.enterpriseRFID.notify.MessageListenerService.startService(MessageListenerService.java:232)
at com.rfidreader.volvo.model.AlienReader.startMessageService(AlienReader.java:99)
at com.rfidreader.volvo.model.AlienReader.<init>(AlienReader.java:73)
at com.rfidreader.volvo.VolvoRfidReaderApplication.lambda$1(VolvoRfidReaderApplication.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

这是启动String引导应用程序时的主要方法:

@Component
@Scope("prototype")
public class AlienReader extends AlienClass1Reader implements 
TagTableListener, MessageListener{

 private String ipaddress;
 private int port;
 private String username;
 private String password;
 private TagTable tagTable = new TagTable();
 private MessageListenerService service;
 private TagTableListener tagTableListener;


 private static final Logger log =LogManager.getLogger(AlienReader.class);

public AlienReader(String ipaddress, int port, String username, String pwd) 
throws UnknownHostException, AlienReaderException, InterruptedException{
    super(ipaddress, port);
    this.ipaddress=ipaddress;
    this.port=port;
    this.username=username;
    this.password=pwd;
    startMessageService();


    }

public void startMessageService() throws UnknownHostException, 
 AlienReaderException, InterruptedException{


    if(ipaddress.equals("222.22.222.22")){
        service=new MessageListenerService(3400);
        service.setMessageListener(this);
    }else if (ipaddress.equals("333.33.333.33")){
        service=new MessageListenerService(3500);
        service.setMessageListener(this);
    }

    try{
        service.startService();
     }catch(IOException e){
         e.printStackTrace();
     }
    log.info("Service has started on : "+ipaddress);
    setReaderConfig();
}

public void setReaderConfig() throws UnknownHostException, AlienReaderException, InterruptedException{
    log.info("Setting up reader");
     this.setUsername(username);
    this.setPassword(password);
      String myIP=InetAddress.getLocalHost().getHostAddress();
    try{
        this.open();
        this.setNotifyAddress(myIP,service.getListenerPort());
        this.setNotifyFormat(AlienClass1Reader.TEXT_FORMAT);
        this.setNotifyTrigger("TrueFalse");
        this.setNotifyMode(AlienClass1Reader.ON);
        this.autoModeReset();
        this.setAutoMode(AlienClass1Reader.ON);
        tagTable.setPersistTime(2000);
        this.close();
        }catch(AlienReaderException e){
            log.error("Reader "+ this.getIPAddress()+" did not connect."+e.getMessage());

        }


}


@Override
public synchronized void messageReceived(Message msg) {
     if(msg instanceof ErrorMessage){
         log.error("Notify error from "+ msg.getReaderIPAddress());

     }else if (msg.getTagCount() == 0){

                log.info("No tags!");
     }else{
        log.info("Message received from: "+msg.getReaderIPAddress());
         String location = msg.getReaderIPAddress();
        Tag[] tagL=msg.getTagList();
        for (int i=0;i<msg.getTagCount(); i++){
            Tag tag = msg.getTag(i);
            this.tagTable.addTag(tag);

            log.info("Tag ID: "+tag.getTagID()+ " Last Seen: "+tag.getRenewTime()+ " Receive Antenna: "+tag.getReceiveAntenna()+ " ip: "+ location);

        }

      }
}

1 个答案:

答案 0 :(得分:1)

这里的主要错误是:

class ExampleFilter(django_filters.FilterSet):

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('kwarg_I_want_to_pass', None)
        super(ExampleFilter, self).__init__(*args, **kwargs)

        self.filters['field_to_filter'].extra.update({
           'queryset': Supplier.objects.filter(related_user=self.user),
           'empty_label': '',
           'help_text': False
        })

    class Meta:
        model = ExampleModel
        fields = ['related_user', 'field_to_filter', ... other fields]

端口一次只能在计算机上使用一件事。因此,例如,如果我有一个spring-boot网络应用程序,并且该应用程序在端口8080上运行,那么我不能一次运行它的两个副本,而又不告诉第二个使用另一个端口,例如8081。

这里有一些逻辑可以选择2个不同的端口:

Could not listen on port 3500. Is that port in use?

不幸的是,在您的for循环中,您好像用相同的IP启动多个读取器(在这种情况下,奇怪的是,该读取器似乎与23或24个结尾的读取器不匹配,但我可能只是缺少了一些内容)。

if(ipaddress.equals("222.2223")){
    service=new MessageListenerService(3400);
    service.setMessageListener(this);
}else if (ipaddress.equals("222.2224")){
    service=new MessageListenerService(3500);
    service.setMessageListener(this);
}

您需要确保每次启动服务时,它都有一个完全唯一的端口,计算机上的其他任何东西都不会使用该端口,否则您将不断看到此错误。

我认为您应该简化问题。只需暂时在每个可运行的设备中运行一个读取器,确保它们具有自己的不同端口(我建议您从头开始手动输入),然后在可行时将其扩展到更多端口。