ActiveMQ KahaDB always locking and waiting - java

I am trying to use ActiveMQ in a relatively simple work queue use case. I have one queue, and have a simple Producer and Consumer.
My question is what am I doing wrong that continuously makes the DB lock?
here is the message I get continously:
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.
I am running the producer and consumer in separate threads at the same time.
Initially, I had the connection at the class level, so I thought that was the problem, but even creating the connection from scratch every call to put and get still causes locking.
I have done research but have failed to find a solution.
I am running ActiveMQ 5.9.0 on windows 7.
Here is what prints out when I start it via cmd prompt:
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'
Here is my java to implement the Queue as ActiveMQ (I have a Queue interface and this is an Impl)
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();
}
}
}
Here are the producers and consumers (simple debugging classes)
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);
}
}
}
any help is appreciated.

do following
kill java process for active mq, open task manager -> go to process -> check for java -> and right click and say end process
then go to <activemq_install_directory>/data/kahadb
and delete lock file which gets generated
try starting activemq again
for me this works every time I face this issue

had the same problem.
there was no other java services / brokers up and running.
the problem was pretty stupid -
make sure that the user you are using has permission to write to the path of the lock file.

The only solution I came across to release this lock is-
Goto services.msc
Stop ActiveMQ forever (let StartUp type be Automatic)
you will see that the lock file disappears
delete the datafolder itself
Restart apache-activemq-5.10.0-bin\apache-activemq-5.10.0\bin\win64\activemq.bat (Not services.msc)
You are good to go

for linux just:
sudo killall java
and start again

Related

Active MQ connection issue

Hi am working on wso2 esb and using Active MQ for message queue.
I have a simple service to place a message in which it call custom java class where it creates a tcp connection and drops a message in queue.
Java code looks like below
package in.esb.custommediators;
import javax.jms.*;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.synapse.ManagedLifecycle;
import org.apache.synapse.MessageContext;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.mediators.AbstractMediator;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.transport.nhttp.NhttpConstants;
import org.json.JSONObject;
import org.json.XML;
public class JMSStoreMediator extends AbstractMediator implements
ManagedLifecycle {
Connection connection;
Session session;
public boolean mediate(MessageContext msgCtx) {
log.info("LogLocation = "+getClass().getName()+",ProxyName = "+msgCtx.getProperty("proxy.name")+
",Usercode = "+msgCtx.getProperty("usercode")+",Clientid = "+msgCtx.getProperty("clientid")+
",requestMsgId = "+msgCtx.getProperty("requestMsgId")+",Position = START");
try {
boolean topic=false;
String jmsuri=""+msgCtx.getProperty("jmsuri");
String t=""+msgCtx.getProperty("topic");
if(t.isEmpty()){
topic=false;
}
else {
topic=Boolean.valueOf(t);
}
ConnectionFactory factory= new ActiveMQConnectionFactory(jmsuri);
connection = factory.createConnection();
connection.start();
log.info("LogLocation = "+getClass().getName()+",JMS connection created :"+connection);
this.session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination=null;
if(!topic)destination= session.createQueue(""+msgCtx.getProperty("jmsqueue"));
else destination= session.createTopic(""+msgCtx.getProperty("jmsqueue"));
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
String xml = ""+msgCtx.getEnvelope().getBody().toStringWithConsume();
if(topic){
JSONObject obj=XML.toJSONObject(xml);
JSONObject ar=obj.getJSONObject("soapenv:Body");
ar.remove("xmlns:soapenv");
xml=ar.toString();
}
TextMessage message = session.createTextMessage(xml);
producer.send(message);
} catch (Exception e) {
log.info("LogLocation = "+getClass().getName()+",Error in storing message in JMS stacktrace is :"+e.toString()+"message is :"+e.getMessage());
e.printStackTrace();
((Axis2MessageContext) msgCtx).setProperty(NhttpConstants.HTTP_SC, 500);
handleException("Error while storing in the message store", msgCtx);
}
finally {
try {
session.close();
if (connection!=null){
log.info("LogLocation = "+getClass().getName()+",JMS connection closing :"+connection);
connection.close();
}
} catch (JMSException e) {
log.info("LogLocation = "+getClass().getName()+",Error in closing JMS connection stacktrace is :"+e.toString());
e.printStackTrace();
}
}
return true;
}
#Override
public void destroy() {
// TODO Auto-generated method stub
}
#Override
public void init(SynapseEnvironment arg0) {
// TODO Auto-generated method stub
}
}
when i call this service to send a message in queue below logs get generated.
[2017-07-29 11:18:35,962] INFO - JMSStoreMediator LogLocation = in.esb.custommediators.JMSStoreMediator,JMS connection created :ActiveMQConnection {id=ID:my-desktop-36442-1501307315570-3:1,clientId=ID:my-desktop-36442-1501307315570-2:1,started=true}
As of now every thing is working good , But when two users try to submit message at the same tire some strange thing happen as shown below
[2017-07-29 11:43:11,948] INFO - JMSStoreMediator LogLocation = in.my.esb.custommediators.JMSStoreMediator,JMS connection created :ActiveMQConnection {id=ID:my-desktop-36442-1501307315570-11:1,clientId=ID:my-desktop-36442-1501307315570-10:1,started=false}
[2017-07-29 11:43:11,963] INFO - JMSStoreMediator LogLocation = in.my.esb.custommediators.JMSStoreMediator,JMS connection created :ActiveMQConnection {id=ID:my-desktop-36442-1501307315570-11:1,clientId=ID:my-desktop-36442-1501307315570-10:1,started=true}
[2017-07-29 11:43:12,068] INFO - JMSStoreMediator LogLocation = in.my.esb.custommediators.JMSStoreMediator,Error in closing JMS connection stacktrace is :org.apache.activemq.ConnectionClosedException: The connection is already closed
Active MQ is creating two connections but using one connection for both the calls and that one connection is getting closed in one of the service call and throwing already closed error in the other service call and the other connection is waiting forever in the connection list of active mq with active status true as shown in the below image and this is also seen in the ESB thread list.
This kind of connections pileup and causing hangs ESB server. Even if i reset this connections from Active MQ ESB threads carry this connection info and only after a restart of ESB the problem get fixed.
Have you read article Extending the Functionality of WSO2 Enterprise Service Bus - Part 1 ?
Important part is Threading Safety. It states, each mediator, including custom, is shared between incoming messages. I recommend to move class variables
Connection connection;
Session session;
to method public boolean mediate(MessageContext msgCtx) since local variables are thread safe
public class JMSStoreMediator extends AbstractMediator implements
ManagedLifecycle {
public boolean mediate(MessageContext msgCtx) {
Connection connection;
Session session;
....
....
rest the same

ActiveMQ JMS : Unknown data type -1

I am using below code for connecting to JMS.I cannot create a connection factory because in PROD env, I am just a consumer and not aware which server it is installed. I have the broker URL,Username and Password.
public class HelloWorldProducer implements Runnable {
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("id972483","octitt123","tcp://ozoi6000:2506");
// Create a Connection
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("gw.oct.request.letterbusinesscommunication.v1.in.q");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
//String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
TextMessage message = session.createTextMessage(getXMLdata());
message.setJMSCorrelationID("Sample_JMS_Test");
// Tell the producer to send the message
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
// Clean up
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
I am getting the error like below:
Caught: javax.jms.JMSException: Disposed due to prior exception
javax.jms.JMSException: Disposed due to prior exception
at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:72)
at org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1413)
at org.apache.activemq.ActiveMQConnection.ensureConnectionInfoSent(ActiveMQConnection.java:1478)
at org.apache.activemq.ActiveMQConnection.start(ActiveMQConnection.java:527)
at HelloWorldProducer.run(HelloWorldProducer.java:25)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.activemq.transport.TransportDisposedIOException: Disposed due to prior exception
at org.apache.activemq.transport.ResponseCorrelator.onException(ResponseCorrelator.java:125)
at org.apache.activemq.transport.TransportFilter.onException(TransportFilter.java:114)
at org.apache.activemq.transport.TransportFilter.onException(TransportFilter.java:114)
at org.apache.activemq.transport.WireFormatNegotiator.onException(WireFormatNegotiator.java:173)
at org.apache.activemq.transport.AbstractInactivityMonitor.onException(AbstractInactivityMonitor.java:344)
at org.apache.activemq.transport.TransportSupport.onException(TransportSupport.java:96)
at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:219)
... 1 more
Caused by: java.io.IOException: Unknown data type: -1
at org.apache.activemq.openwire.OpenWireFormat.doUnmarshal(OpenWireFormat.java:359)
at org.apache.activemq.openwire.OpenWireFormat.unmarshal(OpenWireFormat.java:278)
at org.apache.activemq.transport.tcp.TcpTransport.readCommand(TcpTransport.java:240)
at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:232)
at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:215)
... 1 more
What is configured on the port you are trying to connect to? You see these sorts of error when trying to connect the JMS client to a port that servicing a protocol other than the OpenWire protocol that the ActiveMQ JMS client speaks.
When getting errors like this it is usually a good idea to ask what the broker is actually configured to do

Java Qpid Proton - ActiveMQ broker Cannot assign requested address: bind

I really need an answer to this question that's why I'm editing it.
I have an Apache ActiveMQ broker built inside my connection using this code
Broker.java
public class Broker {
private BrokerService broker;
public Broker(String connector) {
this.broker = new BrokerService();
this.broker.setUseJmx(true);
try {
this.broker.addConnector(connector);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void addConnector(String connector){
try {
this.broker.addConnector(connector);
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() {
try {
this.broker.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public BrokerService getBroker() {
return broker;
}
public void setBroker(BrokerService broker) {
this.broker = broker;
}
}
Here is my problem
I use the Qpid Proton library (available here : Qpid Proton). I have one class to read data which is nearly the example they give you on qpid webiste
package messaging;
import java.io.IOException;
import org.apache.qpid.proton.Proton;
import org.apache.qpid.proton.amqp.messaging.AmqpValue;
import org.apache.qpid.proton.engine.BaseHandler;
import org.apache.qpid.proton.engine.Delivery;
import org.apache.qpid.proton.engine.Event;
import org.apache.qpid.proton.engine.Receiver;
import org.apache.qpid.proton.message.Message;
import org.apache.qpid.proton.reactor.FlowController;
import org.apache.qpid.proton.reactor.Handshaker;
public class AMQPSubscriber extends BaseHandler {
private String broker;
private String topic;
private String port;
public AMQPSubscriber(String broker, String port, String topic) {
this.broker = broker;
this.port = port;
this.topic = topic;
this.add(new Handshaker());
this.add(new FlowController());
}
#Override
public void onReactorInit(Event event) {
try {
event.getReactor().acceptor(broker, Integer.parseInt(port), new AMQPSubscriber(broker, port, topic));
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onDelivery(Event event) {
System.out.println("---------Message Received--------");
Receiver recv = (Receiver) event.getLink();
Delivery delivery = recv.current();
if (delivery.isReadable() && !delivery.isPartial()) {
int size = delivery.pending();
byte[] buffer = new byte[size];
int read = recv.recv(buffer, 0, buffer.length);
recv.advance();
Message msg = Proton.message();
msg.decode(buffer, 0, read);
System.out.println("Subject : " + msg.getProperties().getSubject());
System.out.println("Text : " + ((AmqpValue) msg.getBody()).getValue());
}
}
}
This class is called in the main :
public static void main (String[]args) throws IOException, TimeoutException, InterruptedException{
Broker broker = new Broker("amqp://" + host + ":" + AMQPport);
broker.start();
AMQPSubscriber receiv = new AMQPSubscriber(host, "5672", topic);
Reactor r;
try {
r = Proton.reactor(receiv);
r.run();
} catch (IOException e) {
e.printStackTrace();
}
}
But when i execute this code, i get a
INFO | Loaded the Bouncy Castle security provider.
INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\Users\alexi\Documents\workspace-sts-3.7.3.RELEASE\IOT\activemq-data\localhost\KahaDB]
INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
INFO | KahaDB is version 6
INFO | Recovering from the journal #1:61115
INFO | Recovery replayed 11 operations from the journal in 0.014 seconds.
INFO | PListStore:[C:\Users\alexi\Documents\workspace-sts-3.7.3.RELEASE\IOT\activemq-data\localhost\tmp_storage] started
INFO | Apache ActiveMQ 5.13.3 (localhost, ID:DESKTOP-UK0JIC4-52783-1467025817901-0:1) is starting
INFO | Listening for connections at: amqp://127.0.0.1:5672
INFO | Connector amqp://127.0.0.1:5672 started
INFO | Apache ActiveMQ 5.13.3 (localhost, ID:DESKTOP-UK0JIC4-52783-1467025817901-0:1) started
INFO | For help or more information please see: http://activemq.apache.org
WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: C:\Users\alexi\Documents\workspace-sts-3.7.3.RELEASE\IOT\activemq-data\localhost\KahaDB only has 7792 mb of usable space. - resetting to maximum available disk space: 7792 mb
WARN | Temporary Store limit is 51200 mb (current store usage is 0 mb). The data directory: C:\Users\alexi\Documents\workspace-sts-3.7.3.RELEASE\IOT\activemq-data\localhost only has 7792 mb of usable space. - resetting to maximum available disk space: 7792 mb
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source)
at java.nio.channels.ServerSocketChannel.bind(Unknown Source)
at org.apache.qpid.proton.reactor.impl.AcceptorImpl.<init>(AcceptorImpl.java:102)
at org.apache.qpid.proton.reactor.impl.ReactorImpl.acceptor(ReactorImpl.java:477)
at messaging.AMQPSubscriber.onReactorInit(AMQPSubscriber.java:33)
at org.apache.qpid.proton.engine.BaseHandler.handle(BaseHandler.java:209)
at org.apache.qpid.proton.engine.impl.EventImpl.dispatch(EventImpl.java:108)
at org.apache.qpid.proton.engine.impl.EventImpl.delegate(EventImpl.java:129)
at org.apache.qpid.proton.engine.impl.EventImpl.dispatch(EventImpl.java:114)
at org.apache.qpid.proton.reactor.impl.ReactorImpl.dispatch(ReactorImpl.java:307)
at org.apache.qpid.proton.reactor.impl.ReactorImpl.process(ReactorImpl.java:275)
at org.apache.qpid.proton.reactor.impl.ReactorImpl.run(ReactorImpl.java:343)
at messaging.Main.main(Main.java:98)
This broker works fine when I use MQTT and Paho, I would like it also works with AMQP.
I know bind means the port is already used, but I can't figure out how I could listen on a port where no data is sent.
Thanks for helping me.
Alexi
You get that error when there is another application that has created a server socket listening on the target port you have requested. You need to check that you aren't running another broker instance on that port or have some firewall in place that is blocking access.
I have found a solution.
When i add a connector to the activeMQ embedded broker, it adds it as a TCP which only allows one connection at a time.
So I create a connector as UDP like this :
broker.addConnector("udp://"+host+":"+AMQPport);
This solution is working for me I hope it can help other developers in the future.
Cheers, Alexi

How to get notified when a producer's connection dropped?

I am using an embedded ActiveMQ broker.
My goal is to find a way to detect when an external producer on a Queue lost it's connection.
I am starting the broker like this:
BrokerService broker = new BrokerService();
broker.addConnector("tcp://" + LISTEN_DEVICE_IP + ":" + port);
setLastMessagesPersistent(broker);
broker.start();
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection.start();
And after that I tried to add a TransportListener:
((ActiveMQConnection) connection).addTransportListener(new TransportListener() {
public void transportResumed() {
System.out.println("resumed");
}
public void transportInterupted() {
System.out.println("interrupted");
}
public void onException(IOException arg0) {
System.out.println("ioexception: " + arg0);
}
public void onCommand(Object arg0) {
System.out.println("command: " + arg0);
}
});
I also register a consumer and ProducerListener like this:
Destination dest = session.createQueue(queuename);
MessageConsumer consumer = session.createConsumer(dest);
ProducerEventSource source = new ProducerEventSource(connection, dest);
System.out.println("Setting Producer Listener");
source.setProducerListener(prodevent -> {
System.out.println("producer status: " + prodevent.isStarted());
});
// Gets called from inside the broker's Thread and somehow causes deadlocks if I don't invoke this from the outside
new Thread(() -> {
try {
consumer.setMessageListener(new NetworkEventPlayerAdapter(objectMapper, event, gameEventManager, playerID));
} catch (Exception e) {
e.printStackTrace();
}
}).start();
Unfortunately neither the TransportListener, nor the ProducerListener give me any output when I forcefully quit another application that previously was added as a producer (Alt+F4). The broker surely notices though:
WARN | Transport Connection to: tcp://127.0.0.1:58988 failed: java.net.SocketException: Connection reset
WARN | Transport Connection to: tcp://127.0.0.1:58986 failed: java.net.SocketException: Connection reset
But I don't find a way to get a callback on those events in Java.
I also tried setting a custom IOExceptionHandler in the broker and adding an ExceptionListener to the connection. They also get never called.
You can use The Advisory Topics ActiveMQ.Advisory.Connection or even the ActiveMQ.Advisory.Producer.Queue ActiveMQ.Advisory.Producer.Topic , these provide the stats on connections of number of producers , check this link http://activemq.apache.org/advisory-message.html
a possible way is to parse the log-output for Connection reset
WARN | Transport Connection to: tcp://127.0.0.1:58988 failed:
java.net.SocketException: Connection reset
WARN | Transport
Connection to: tcp://127.0.0.1:58986 failed: java.net.SocketException:
Connection reset
Because the socket is implemented in activemq you would have to add the ExceptionListener there to write your own exception handling routine ...

ActiveMQ and embedded broker

EDIT: Rephrased the question:
I want to use ActiveMQ as a messenger service between my server and client applications.
I am trying to set up an embedded broker (i.e. not a separate process) within the server to handle the produced messages for my clients to consume. This queue is persisted.
The broker initialisation as follows:
BrokerService broker = new BrokerService();
KahaPersistenceAdapter adaptor = new KahaPersistenceAdapter();
adaptor.setDirectory(new File("activemq"));
broker.setPersistenceAdapter(adaptor);
broker.setUseJmx(true);
broker.addConnector("tcp://localhost:61616");
broker.start();
After tinkering, I ended up with the server part being:
public static class HelloWorldProducer implements Runnable {
public void run() {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost"); // apparently the vm part is all i need
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("TEST.FOO");
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
TextMessage message = session.createTextMessage(text);
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
}
The client is very similar and looks like this:
public static class HelloWorldConsumer implements Runnable, ExceptionListener {
public void run() {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost");
Connection connection = connectionFactory.createConnection(); // exception happens here...
connection.start();
connection.setExceptionListener(this);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("TEST.FOO");
MessageConsumer consumer = session.createConsumer(destination);
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("*****Received: " + text);
} else {
System.out.println("*****Received obj: " + message);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
The main method simply starts each of these in a thread to start producing/receiving messages.
...but I am running into the following with the start of each thread:
2013-01-24 07:54:31,271 INFO [org.apache.activemq.broker.BrokerService] Using Persistence Adapter: AMQPersistenceAdapter(activemq-data/localhost)
2013-01-24 07:54:31,281 INFO [org.apache.activemq.store.amq.AMQPersistenceAdapter] AMQStore starting using directory: activemq-data/localhost
2013-01-24 07:54:31,302 INFO [org.apache.activemq.kaha.impl.KahaStore] Kaha Store using data directory activemq-data/localhost/kr-store/state
2013-01-24 07:54:31,339 INFO [org.apache.activemq.store.amq.AMQPersistenceAdapter] Active data files: []
2013-01-24 07:54:31,445 DEBUG [org.apache.activemq.broker.jmx.ManagementContext] Probably not using JRE 1.4: mx4j.tools.naming.NamingService
2013-01-24 07:54:31,450 DEBUG [org.apache.activemq.broker.jmx.ManagementContext] Failed to create local registry
java.rmi.server.ExportException: internal error: ObjID already in use
at sun.rmi.transport.ObjectTable.putTarget(ObjectTable.java:186)
at sun.rmi.transport.Transport.exportObject(Transport.java:92)
at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:247)
at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411)
at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147)
<snip....>
It seems like the messages are produced and consumed successfully (the other issues I previously posted about was resolved), but the above exception is worrying me.
EDIT: During broker shutdown, I am now also greeted by the following:
2013-01-25 08:40:17,486 DEBUG [org.apache.activemq.transport.failover.FailoverTransport] Transport failed with the following exception:
java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:392)
at org.apache.activemq.openwire.OpenWireFormat.unmarshal(OpenWireFormat.java:269)
at org.apache.activemq.transport.tcp.TcpTransport.readCommand(TcpTransport.java:210)
at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:202)
at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:185)
at java.lang.Thread.run(Thread.java:722)
You can embed a broker into your code in a number of ways, much of which is documented here. You may want to try upgrading you version since what you are using appears to be quite old as it defaulting to the now deprecated AMQ Store instead of the newer KahaDB store. You might be having issues because of a race between the client threads as they use the different connection factories which could race to create in VM brokers. If you set the create=false option on the producer and ensure the consumer thread starts after that could address the issue, or you could create the VM broker ahead of time and the add create=false to both thread's and that might do the trick.
BrokerService broker = new BrokerService();
// configure the broker
broker.setBrokerName("localhost");
broker.setUseJmx(false);
broker.start();
And then in the client code just attach via this connection factory configuration.
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?create=false");
When I run your code, I got the below exception:
javax.jms.JMSException: Could not connect to broker URL: tcp://localhost.
Reason java.lang.IllegalArgumentException: port out of range:-1
Your broker is running and listening to port 61616, so any client which tries to connect to broker need to have the port in its URL.
The client code tries to connect to localhost but doesn't mention the port to which it has to connect.
Both the producer and consumer code needs to be fixed.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost");
To
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
After fixing the port, I was able to run your code.

Categories

Resources