Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ActiveMQ始终处于锁定和等待状态_Java_Multithreading_Activemq - Fatal编程技术网

Java ActiveMQ始终处于锁定和等待状态

Java ActiveMQ始终处于锁定和等待状态,java,multithreading,activemq,Java,Multithreading,Activemq,我试图在一个相对简单的工作队列用例中使用ActiveMQ。我有一个队列,有一个简单的生产者和消费者。 我的问题是,我到底做错了什么,导致数据库一直处于锁定状态? 以下是我不断得到的信息: 14/04/05 18:14:13 INFO store.SharedFileLocker: Database activemq-data\localhost\KahaDB\lock is locked... waiting 10 seconds for the database to be unlocked.

我试图在一个相对简单的工作队列用例中使用ActiveMQ。我有一个队列,有一个简单的生产者和消费者。 我的问题是,我到底做错了什么,导致数据库一直处于锁定状态? 以下是我不断得到的信息:

14/04/05 18:14:13 INFO store.SharedFileLocker: Database activemq-data\localhost\KahaDB\lock is locked... waiting 10 seconds for the database to be unlocked. Reason: java.io.IOException: File 'activemq-data\localhost\KahaDB\lock' could not be locked.
我同时在不同的线程中运行生产者和消费者。 最初,我在类级别有连接,所以我认为这就是问题所在,但即使从头开始创建连接,每次调用put和get仍然会导致锁定

我做过研究,但没有找到解决办法。 我正在windows 7上运行ActiveMQ 5.9.0

以下是通过cmd提示符启动时打印的内容:

C:\activemq\apache-activemq-5.9.0\bin>activemq
Java Runtime: Oracle Corporation 1.7.0_40 C:\Program Files\Java\jre7
  Heap sizes: current=1005568k  free=995061k  max=1005568k
    JVM args: -Dcom.sun.management.jmxremote -Xms1G -Xmx1G -Djava.util.logging.config.file=logging.properties -Dhawtio.realm=activemq -Dhawtio.role=admins -Dhawtio.rolePrincipalCla
vemq.jaas.GroupPrincipal -Djava.security.auth.login.config=C:\activemq\apache-activemq-5.9.0\bin\..\conf\login.config -Dactivemq.classpath=C:\activemq\apache-activemq-5.9.0\bin\..\
che-activemq-5.9.0\bin\../conf;C:\activemq\apache-activemq-5.9.0\bin\../conf; -Dactivemq.home=C:\activemq\apache-activemq-5.9.0\bin\.. -Dactivemq.base=C:\activemq\apache-activemq-5
mq.conf=C:\activemq\apache-activemq-5.9.0\bin\..\conf -Dactivemq.data=C:\activemq\apache-activemq-5.9.0\bin\..\data -Djava.io.tmpdir=C:\activemq\apache-activemq-5.9.0\bin\..\data\t
Extensions classpath:
  [C:\activemq\apache-activemq-5.9.0\bin\..\lib,C:\activemq\apache-activemq-5.9.0\bin\..\lib\camel,C:\activemq\apache-activemq-5.9.0\bin\..\lib\optional,C:\activemq\apache-activemq
b,C:\activemq\apache-activemq-5.9.0\bin\..\lib\extra]
ACTIVEMQ_HOME: C:\activemq\apache-activemq-5.9.0\bin\..
ACTIVEMQ_BASE: C:\activemq\apache-activemq-5.9.0\bin\..
ACTIVEMQ_CONF: C:\activemq\apache-activemq-5.9.0\bin\..\conf
ACTIVEMQ_DATA: C:\activemq\apache-activemq-5.9.0\bin\..\data
Loading message broker from: xbean:activemq.xml
 INFO | Refreshing org.apache.activemq.xbean.XBeanBrokerFactory$1@5bf2a8f5: startup date [Sat Apr 05 17:42:42 EDT 2014]; root of context hierarchy
 INFO | PListStore:[C:\activemq\apache-activemq-5.9.0\bin\..\data\localhost\tmp_storage] started
 INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\activemq\apache-activemq-5.9.0\bin\..\data\kahadb]
 INFO | KahaDB is version 5
 INFO | Recovering from the journal ...
 INFO | Recovery replayed 6935 operations from the journal in 0.416 seconds.
 INFO | Apache ActiveMQ 5.9.0 (localhost, ID:Owner-PC-49614-1396734165637-0:1) is starting
 INFO | Listening for connections at: tcp://Owner-PC:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600
 INFO | Connector openwire started
 INFO | Listening for connections at: amqp://Owner-PC:5673?maximumConnections=1000&wireFormat.maxFrameSize=104857600
 INFO | Connector amqp started
 INFO | Listening for connections at: stomp://Owner-PC:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600
 INFO | Connector stomp started
 INFO | Listening for connections at: mqtt://Owner-PC:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600
 INFO | Connector mqtt started
 INFO | Listening for connections at ws://Owner-PC:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600
 INFO | Connector ws started
 INFO | Apache ActiveMQ 5.9.0 (localhost, ID:Owner-PC-49614-1396734165637-0:1) started
 INFO | For help or more information please see: http://activemq.apache.org
 INFO | Welcome to hawtio 1.2-M23 : http://hawt.io/ : Don't cha wish your console was hawt like me? ;-)
 INFO | Starting hawtio authentication filter, JAAS realm: "activemq" authorized role: "admins" role principal classes: "org.apache.activemq.jaas.GroupPrincipal"
 INFO | Using file upload directory: C:\activemq\apache-activemq-5.9.0\bin\..\data\tmp\uploads
 INFO | jolokia-agent: Using access restrictor classpath:/jolokia-access.xml
 INFO | ActiveMQ WebConsole available at http://localhost:8161/
 INFO | Initializing Spring FrameworkServlet 'dispatcher'
下面是我的java,它将队列实现为ActiveMQ(我有一个队列接口,这是一个Impl)

import com.google.gson.gson;
导入java.util.Set;
导入javax.jms.Connection;
导入javax.jms.DeliveryMode;
导入javax.jms.Destination;
导入javax.jms.ExceptionListener;
导入javax.jms.jmsception;
导入javax.jms.MessageConsumer;
导入javax.jms.MessageProducer;
导入javax.jms.Session;
导入javax.jms.TextMessage;
导入org.apache.activemq.ActiveMQConnectionFactory;
公共类ActiveMQImpl实现队列,ExceptionListener{
私有字符串主机;
私有字符串用户;
私有字符串pw;
公共void init(){
}
public void close(){
}
公共消息获取(){
Message outMessage=null;
ActiveMQConnectionFactory connectionFactory=null;
连接=空;
试一试{
if(连接==null){
connectionFactory=new-ActiveMQConnectionFactory(“vm://localhost”);
//创建连接
connection=connectionFactory.createConnection();
}
connection.start();
//创建会话
会话会话=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
//创建目标(主题或队列)
目的地=session.createQueue(“工作”);
//从会话到主题或队列创建MessageConsumer
MessageConsumer=session.createConsumer(目的地);
//等待消息
javax.jms.Message Message=consumer.receive(1000);
如果(文本消息的消息实例){
text消息text消息=(text消息)消息;
String text=textMessage.getText();
outMessage=new Gson().fromJson(文本,Message.class);
//System.out.println(“接收:+文本”);
}否则{
//System.out.println(“接收:+消息”);
}
consumer.close();
//commit();
session.close();
//connection.close();
}捕获(例外e){
System.out.println(“捕获:+e”);
e、 printStackTrace();
}
返回消息;
}
公共作废put(消息中的消息){
试一试{
ActiveMQConnectionFactory connectionFactory=null;
连接prodConnection=null;
connectionFactory=new-ActiveMQConnectionFactory(“vm://localhost”);
prodConnection=connectionFactory.createConnection();
prodConnection.start();
Session Session=prodConnection.createSession(false,Session.AUTO_-ACKNOWLEDGE);
目的地=session.createQueue(“工作”);
MessageProducer=session.createProducer(目的地);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
制作人:setTimeToLive(60000);
//创建一条消息
字符串text=inMessage.toString();
TextMessage=session.createTextMessage(text);
//告诉制作人发送消息
System.out.println(“发送消息:+文本”);
生产者。发送(消息);
producer.close();
//commit();
session.close();
prodConnection.close();
}捕获(例外e){
System.out.println(“捕获:+e”);
OneException(空);
e、 printStackTrace();
}
}
公共无效OneException(JMSExException jmse){
//将此发送到错误通道对象。。。
System.out.println(jmse);
}
公共作废放置(设置消息){
试一试{
ActiveMQConnectionFactory connectionFactory=null;
连接=空;
连接prodConnection=null;
if(连接==null){
connectionFactory=new-ActiveMQConnectionFactory(“vm://localhost?jms.useAncynsend=true”);
//创建连接
connection=connectionFactory.createConnection(“admin”、“admin”);
}
connection.start();
//创建会话
会话会话=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
//创建目标(主题或队列)
目的地=session.createQueue(“工作”);
//创建从会话到主题或队列的MessageProducer
MessageProducer=session.createProducer(目的地);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
//创建一条消息
对于(消息中的消息:消息){
字符串text=inMessage.toString();
TextMessage=session.createTextMessage(text);
//告诉制作人发送消息
System.out.println(“发送消息:+文本”);
生产者。发送(消息);
}
producer.close();
//commit();
session.close();
//connection.close();
}捕获(例外e){
System.out.println(“捕获:+e”);
OneException(空);
e、 printStackTrace();
}
}
}
以下是生产者和消费者(简单调试类)

公共类制作人{
公共静态void main(字符串[]args){
Queue q=QueueFactory.create(QueueType.ACTIVEMQ);
试一试{
对于(int i=0;i<10;i++){
q、 放置(新消息(“testimpl”+i,“whatever”,i));
睡眠(1000);
}
}捕获(中断异常例外){
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
公共类消费者{
公共静态vo
import com.google.gson.Gson;
import java.util.Set;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQImpl implements Queue, ExceptionListener {

  private String host;
  private String user;
  private String pw;

  public void init() {
  }

  public void close() { 
  }

  public Message get() {
    Message outMessage = null;
    ActiveMQConnectionFactory connectionFactory = null;
    Connection connection = null;  
    try {
      if (connection == null) {
        connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

        // Create a Connection
        connection = connectionFactory.createConnection();
      }
      connection.start();

      // Create a Session
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Create the destination (Topic or Queue)
      Destination destination = session.createQueue("work");
      // Create a MessageConsumer from the Session to the Topic or Queue
      MessageConsumer consumer = session.createConsumer(destination);

      // Wait for a message
      javax.jms.Message message = consumer.receive(1000);

      if (message instanceof TextMessage) {
        TextMessage textMessage = (TextMessage) message;
        String text = textMessage.getText();
        outMessage = new Gson().fromJson(text, Message.class);
        //   System.out.println("Received: " + text);
      } else {
        // System.out.println("Received: " + message);
      }

      consumer.close();
      //  session.commit();
      session.close();
      //connection.close();
    } catch (Exception e) {
      System.out.println("Caught: " + e);
      e.printStackTrace();
    }
    return outMessage;
  }

  public void put(Message inMessage) {
    try {
      ActiveMQConnectionFactory connectionFactory = null;


      Connection prodConnection = null;
      connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

      prodConnection = connectionFactory.createConnection();

      prodConnection.start();

      Session session = prodConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      Destination destination = session.createQueue("work");

      MessageProducer producer = session.createProducer(destination);
      producer.setDeliveryMode(DeliveryMode.PERSISTENT);
      producer.setTimeToLive(60000);
      // Create a messages
      String text = inMessage.toString();
      TextMessage message = session.createTextMessage(text);

      // Tell the producer to send the message
      System.out.println("Sent message: " + text);
      producer.send(message);
      producer.close();
      // session.commit();
      session.close();
      prodConnection.close();
    } catch (Exception e) {
      System.out.println("Caught: " + e);
      onException(null);
      e.printStackTrace();
    }
  }

  public void onException(JMSException jmse) {
    //send this to the error channel object... 
    System.out.println(jmse);
  }

  public void put(Set<Message> messages) {
    try {
  ActiveMQConnectionFactory connectionFactory=null;
  Connection connection=null;

  Connection prodConnection=null;
      if (connection == null) {
        connectionFactory = new ActiveMQConnectionFactory("vm://localhost?jms.useAsyncSend=true");

        // Create a Connection
        connection = connectionFactory.createConnection("admin", "admin");
      }

      connection.start();

      // Create a Session
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Create the destination (Topic or Queue)
      Destination destination = session.createQueue("work");
      // Create a MessageProducer from the Session to the Topic or Queue
      MessageProducer producer = session.createProducer(destination);
      producer.setDeliveryMode(DeliveryMode.PERSISTENT);

      // Create a messages
      for (Message inMessage : messages) {
        String text = inMessage.toString();
        TextMessage message = session.createTextMessage(text);

        // Tell the producer to send the message
        System.out.println("Sent message: " + text);
        producer.send(message);
      }
      producer.close();
      // session.commit();
      session.close();
      //connection.close();
    } catch (Exception e) {
      System.out.println("Caught: " + e);
      onException(null);
      e.printStackTrace();
    }
  }

}
public class Producer {

  public static void main(String[] args) {
    Queue q = QueueFactory.create(QueueType.ACTIVEMQ);
    try {
      for (int i = 0; i < 10; i++) {

        q.put(new Message("testimpl" + i, "whatever", i));
        Thread.sleep(1000);

      }
    } catch (InterruptedException ex) {
      Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
    }

  }
}


public class Consumer {

  public static void main(String[] args) {

    Queue q = QueueFactory.create(QueueType.ACTIVEMQ);
    try {
      while (true) {

        Message get = q.get();
        System.out.println(get);
        Thread.sleep(1000);

      }
    } catch (InterruptedException ex) {
      Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}
sudo killall java