问题
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!
Here's the error:
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)
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);
}
}
}
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();
}
回答1:
The main error here is:
Could not listen on port 3500. Is that port in use?
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.
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);
}
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.
来源:https://stackoverflow.com/questions/52120433/app-keeps-trying-to-restart-message-service-how-do-i-run-the-message-service-on