应用一直尝试重启消息服务,如何只运行消息服务一次?
App keeps trying to restart message service, how do I run the message service only once?
我有一个运行带有 2 个可运行程序的执行程序服务的应用程序,这些可运行程序启动进程以从 rfid reader 接收消息。此 reader 从 messagelistener 收到一条消息,其中包含标签经过 reader 后的标签信息。我收到了标签信息,但在它 运行 时收到错误,因为它一直在尝试重新启动 messagelistenerservice。试图只添加需要查看的代码。我如何只启动一次 messagelistener 服务,这样我就不会收到此错误。谢谢!
这是错误:
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(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)
这是 reader:
的 @Component
@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 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 web-app,并且它 运行 在端口 8080 上,我不能在不告知第二个使用不同的端口,如 8081。
你在这里有一些选择 2 个不同端口的逻辑:
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 启动了多个 reader(奇怪的是,在这种情况下,这似乎与结尾的 23 或 24 不匹配,但我'我可能只是遗漏了什么)。
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){
}
您需要确保每次启动服务时,它都有一个完全唯一的端口,未被计算机上的任何其他东西使用,否则您会一直看到此错误。
我认为你应该简化问题。现在每个 运行nable 中只有 运行 个 reader,确保它们有自己不同的端口(我建议你从一开始就手动传入),并将其扩展到更多一旦成功。
我有一个运行带有 2 个可运行程序的执行程序服务的应用程序,这些可运行程序启动进程以从 rfid reader 接收消息。此 reader 从 messagelistener 收到一条消息,其中包含标签经过 reader 后的标签信息。我收到了标签信息,但在它 运行 时收到错误,因为它一直在尝试重新启动 messagelistenerservice。试图只添加需要查看的代码。我如何只启动一次 messagelistener 服务,这样我就不会收到此错误。谢谢!
这是错误:
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(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)
这是 reader:
的 @Component@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 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 web-app,并且它 运行 在端口 8080 上,我不能在不告知第二个使用不同的端口,如 8081。
你在这里有一些选择 2 个不同端口的逻辑:
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 启动了多个 reader(奇怪的是,在这种情况下,这似乎与结尾的 23 或 24 不匹配,但我'我可能只是遗漏了什么)。
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){
}
您需要确保每次启动服务时,它都有一个完全唯一的端口,未被计算机上的任何其他东西使用,否则您会一直看到此错误。
我认为你应该简化问题。现在每个 运行nable 中只有 运行 个 reader,确保它们有自己不同的端口(我建议你从一开始就手动传入),并将其扩展到更多一旦成功。