amandalv 2009-10-14 10:47
浏览 624
已采纳

activeMQ代码在eclipse里运行出现java.net.BindException: Address already in use: JVM_Bind

大家好!我最近在研究activeMQ,我把一些网上的例子在eclipse里运行(没有用spring,觉得spring太麻烦了 先不考虑)

但是第一次运行,结果正常,再运行一遍,就会出现如下error

[color=red]java.io.IOException: Failed to bind to server socket: tcp://localhost:61616 due to: java.net.BindException: Address already in use: JVM_Bind
at org.apache.activemq.util.IOExceptionSupport.create(IOExceptionSupport.java:33)
at org.apache.activemq.transport.tcp.TcpTransportServer.bind(TcpTransportServer.java:141)
at org.apache.activemq.transport.tcp.TcpTransportFactory.doBind(TcpTransportFactory.java:60)
at org.apache.activemq.transport.TransportFactory.bind(TransportFactory.java:131)
at org.apache.activemq.broker.BrokerService.createTransportConnector(BrokerService.java:1712)
at org.apache.activemq.broker.BrokerService.addConnector(BrokerService.java:217)
at org.apache.activemq.broker.BrokerService.addConnector(BrokerService.java:207)
at homework.Test.main(Test.java:18)
Caused by: java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.(ServerSocket.java:185)
at java.net.ServerSocket.(ServerSocket.java:141)
at javax.net.DefaultServerSocketFactory.createServerSocket(ServerSocketFactory.java:162)
at org.apache.activemq.transport.tcp.TcpTransportServer.bind(TcpTransportServer.java:134)
... 6 more
2009-10-14 9:55:05 org.apache.activemq.transport.failover.FailoverTransport doReconnect
信息: Successfully connected to tcp://localhost:61616[/color]Consumer:->Begin listening...
[color=red]2009-10-14 9:55:06 org.apache.activemq.transport.failover.FailoverTransport doReconnect[/color]
[color=red]信息: Successfully connected to tcp://localhost:61616[/color]
Producer:->Sending message: Hello, world!
Producer:->Message sent complete!
Producer:->Closing connection
Consumer:->Received: Hello, world!

Consumer:->Closing connection

虽然结果还是能出来,但是java.io.IOException: Failed to bind to server socket: tcp://localhost:61616 due to: java.net.BindException: Address already in use: JVM_Bind不知道怎么解决,一定要重启eclipse才不报错。
我main函数的代码如下
package homework;

import javax.jms.JMSException;

import org.apache.activemq.broker.BrokerService;

public class Test{

/** //*

  • @param args
    */
    public static void main(String[] args) throws JMSException, Exception {
    // TODO Auto-generated method stub

    BrokerService broker = new BrokerService();
     broker.setUseJmx(true);
    try {
        broker.addConnector("tcp://localhost:61616");
        broker.start();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    ConsumerTool consumer = new ConsumerTool();
    ProducerTool producer = new ProducerTool();
    // 开始监听
    consumer.consumeMessage();

    // 延时500毫秒之后发送消息
    Thread.sleep(500);
    producer.produceMessage("Hello, world!");
    producer.close();

    // 延时500毫秒之后停止接受消息
    Thread.sleep(500);
    consumer.close();
    broker.stop();
    }
    }
    请指点 谢谢!!!是要关掉什么服务吗?怎么关呢?如有需要 我把producerTool和ProducerTool的代码也贴上来

[b]问题补充:[/b]
ProducerTool和ConsumerTool这两个类我是从网上找的

ProducerTool

package homework;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ProducerTool{

private String user = ActiveMQConnection.DEFAULT_USER;

private String password = ActiveMQConnection.DEFAULT_PASSWORD;

private String url = ActiveMQConnection.DEFAULT_BROKER_URL;

private String subject = "TOOL.DEFAULT";

private Destination destination = null;

private Connection connection = null;

private Session session = null;

private MessageProducer producer = null;

// 初始化
private void initialize() throws JMSException, Exception{
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}

// 发送消息
public void produceMessage(String message) throws JMSException, Exception{
initialize();
TextMessage msg = session.createTextMessage(message);
connection.start();
System.out.println("Producer:->Sending message: " + message);
producer.send(msg);
System.out.println("Producer:->Message sent complete!");
}

// 关闭连接
public void close() throws JMSException{
System.out.println("Producer:->Closing connection");
if (producer != null)
producer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
}

}

ConsumerTool
package homework;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.MessageListener;
import javax.jms.Message;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ConsumerTool implements MessageListener{

private String user = ActiveMQConnection.DEFAULT_USER;

private String password = ActiveMQConnection.DEFAULT_PASSWORD;

private String url = ActiveMQConnection.DEFAULT_BROKER_URL;

private String subject = "TOOL.DEFAULT";

private Destination destination = null;

private Connection connection = null;

private Session session = null;

private MessageConsumer consumer = null;

// 初始化
private void initialize() throws JMSException, Exception{
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
consumer = session.createConsumer(destination);

}

// 消费消息
public void consumeMessage() throws JMSException, Exception{
initialize();
connection.start();

System.out.println("Consumer:->Begin listening...");
// 开始监听
consumer.setMessageListener(this);
// Message message = consumer.receive();
}

// 关闭连接
public void close() throws JMSException{
System.out.println("Consumer:->Closing connection");
if (consumer != null)
consumer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
}

// 消息处理函数
public void onMessage(Message message){
try {
if (message instanceof TextMessage){
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
System.out.println("Consumer:->Received: " + msg);
} else {
System.out.println("Consumer:->Received: " + message);
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第一次运行代码是好的,再运行一遍就会说java.io.IOException: Failed to bind to server socket: tcp://localhost:61616 due to: java.net.BindException: Address already in use: JVM_Bind
一定要重启eclipse才行 好郁闷啊 8)

  • 写回答

2条回答 默认 最新

  • weixin_42518082 2009-10-14 18:35
    关注

    我运行了你的代码, 没有发现问题啊....

    另:你要等等程序完全退出了,才能再次运行.不然就可能会出现你说的情况..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题