应用程序一直尝试重新启动消息服务,如何只运行一次消息服务? [英] App keeps trying to restart message service, how do I run the message service only once?

查看:104
本文介绍了应用程序一直尝试重新启动消息服务,如何只运行一次消息服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app that runs an executor service with 2 runnables that start the process to receive messages from a rfid reader. This reader receives a message from the messagelistener that contains the tag information after the tag has gone by the reader. I receive the tag information but I receive an error while its running bc it keeps trying to restart the messagelistenerservice. Tried to only add the code that needs to be seen. How do I just start the messagelistener service once so I don't get this error. Thanks!

这是错误:

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)

这是供读者使用的@Component:

Here's the @Component that for the reader:

@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);

        }

      }
}

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

Here's the main method when starting the String boot app:

public static void main(String[] args) throws UnknownHostException, AlienReaderException, InterruptedException {
    //SpringApplication.run(VolvoRfidReaderApplication.class, args);
    ConfigurableApplicationContext run = SpringApplication.run(VolvoRfidReaderApplication.class, args);
    VolvoRfidReaderApplication app = run.getBean(VolvoRfidReaderApplication.class);
    app.run();
}

private void run(){
    Runnable r1= () -> {
        for(int i=0;i<30;++i){
            //System.out.println("task 1");
            try {
                new AlienReader("222.22.222.22", 22, "username","password");
            } catch (UnknownHostException | AlienReaderException | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try{
                Thread.sleep(1000);
            }catch(Exception ignored){
        }
    }
};

    Runnable r2= () -> {
        for(int i=0;i<30;++i){
            //System.out.println("task 2");
            try {
                new AlienReader("333.33.333.33", 22, "username","password");
            } catch (UnknownHostException | AlienReaderException | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try{
                Thread.sleep(1000);
            }catch(Exception ignored){
        }
    }
    };

        ExecutorService service = Executors.newFixedThreadPool(2);
        service.submit(r1);
        service.submit(r2);


        service.shutdown();
}

推荐答案

此处的主要错误是:

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

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

Ports can only be used by one thing on a computer at a time. So, if I have a spring-boot web-app, for example, and it runs on port 8080, I can't run two copies of it at once without telling the second one to use a different port like 8081.

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

You have some logic here to choose 2 diferent ports:

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);
}

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

Unfortunately, in your for loops it looks like you start multiple readers with the same IP (which strangely in this case doesn't seem to match the the 23 or 24 ending ones, but I'm probably just missing something).

for(int i=0;i<30;++i){
    //System.out.println("task 1");
    try {
        new AlienReader("222.222", 22, "username","password");
    } catch (UnknownHostException | AlienReaderException | InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try{
        Thread.sleep(1000);
    }catch(Exception ignored){
}

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

You need to make sure that every time a service is started, it has a completely unique port not used by any other thing on the computer, or you'll keep seeing this error.

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

I think you should simplify the problem. Just run one reader in each runnable for now, make sure they have their own distinct ports (which I suggest you pass in manually from the beginning), and scale it up to more once that works.

这篇关于应用程序一直尝试重新启动消息服务,如何只运行一次消息服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆