Activemq VM Protocol - BrokerService Error - activemq

I am getting an error while opening connection to the broker service with VM protocol.
BrokerService broker = new BrokerService();
try {
broker.setPersistent(true);
broker.setUseJmx(false);
broker.setBrokerName("broker1");
broker.start();
while(true){
}
} catch (Exception e) {
e.printStackTrace();
}
And here is my producer;
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://broker1?create=false");
Connection connection = null;
Session session = null;
try {
connection = connectionFactory.createConnection();
connection.start();
I am getting this error;
javax.jms.JMSException: Could not create Transport. Reason: java.io.IOException: Broker named 'broker1' does not exist.
at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:35)
at org.apache.activemq.ActiveMQConnectionFactory.createTransport(ActiveMQConnectionFactory.java:254)
at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:267)
at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:239)
at org.apache.activemq.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:185)
at Client.main(Client.java:22)

Tim you are right, connection was not created within the same jvm.I have realized my mistake

Related

What is the use case of BrokerService in ActiveMQ and how to use it correctly

I am new about ActiveMQ. I'm trying to study and check how it works by checking the example code provided by Apache at this link:-
http://activemq.apache.org/how-should-i-implement-request-response-with-jms.html
public class Server implements MessageListener {
private static int ackMode;
private static String messageQueueName;
private static String messageBrokerUrl;
private Session session;
private boolean transacted = false;
private MessageProducer replyProducer;
private MessageProtocol messageProtocol;
static {
messageBrokerUrl = "tcp://localhost:61616";
messageQueueName = "client.messages";
ackMode = Session.AUTO_ACKNOWLEDGE;
}
public Server() {
try {
//This message broker is embedded
BrokerService broker = new BrokerService();
broker.setPersistent(false);
broker.setUseJmx(false);
broker.addConnector(messageBrokerUrl);
broker.start();
} catch (Exception e) {
System.out.println("Exception: "+e.getMessage());
//Handle the exception appropriately
}
//Delegating the handling of messages to another class, instantiate it before setting up JMS so it
//is ready to handle messages
this.messageProtocol = new MessageProtocol();
this.setupMessageQueueConsumer();
}
private void setupMessageQueueConsumer() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(messageBrokerUrl);
Connection connection;
try {
connection = connectionFactory.createConnection();
connection.start();
this.session = connection.createSession(this.transacted, ackMode);
Destination adminQueue = this.session.createQueue(messageQueueName);
//Setup a message producer to respond to messages from clients, we will get the destination
//to send to from the JMSReplyTo header field from a Message
this.replyProducer = this.session.createProducer(null);
this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
//Set up a consumer to consume messages off of the admin queue
MessageConsumer consumer = this.session.createConsumer(adminQueue);
consumer.setMessageListener(this);
} catch (JMSException e) {
System.out.println("Exception: "+e.getMessage());
}
}
public void onMessage(Message message) {
try {
TextMessage response = this.session.createTextMessage();
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String messageText = txtMsg.getText();
response.setText(this.messageProtocol.handleProtocolMessage(messageText));
}
//Set the correlation ID from the received message to be the correlation id of the response message
//this lets the client identify which message this is a response to if it has more than
//one outstanding message to the server
response.setJMSCorrelationID(message.getJMSCorrelationID());
//Send the response to the Destination specified by the JMSReplyTo field of the received message,
//this is presumably a temporary queue created by the client
this.replyProducer.send(message.getJMSReplyTo(), response);
} catch (JMSException e) {
System.out.println("Exception: "+e.getMessage());
}
}
public static void main(String[] args) {
new Server();
}
}
My confusion about the messageBrokerUrl = "tcp://localhost:61616"; You know ActiveMQ service is running on port 61616 by default. Why does this example chooses same port. If I try to run the code thows eception as:
Exception: Failed to bind to server socket: tcp://localhost:61616 due to: java.net.BindException: Address already in use: JVM_Bind
Perhaps if I change the port number, I can execute the code.
Please let me know why it is like this in the example and how to work with BrokerService.
The BrokerService in this example is trying to create an in memory ActiveMQ broker for use in the example. Given the error you are seeing I'd guess you already have an ActiveMQ broker running on the machine that is bound to port 61616 as that's the default port and thus the two are conflicting. You could either stop the external broker and run the example or modify the example to not run the embedded broker and just rely on your external broker instance.
Embedded brokers are great for unit testing or for creating examples that don't require the user to have a broker installed and running.

javax.jms.InvalidClientIDException: Broker: localhost - Client: FS_Proceduer already connected from /127.0.0.1:port

How do you resolve this JMSException? Thanks!
Broker: localhost - Client: FS_Proceduer already connected
javax.jms.InvalidClientIDException: Broker: localhost - Client: FS_Proceduer already connected from /127.0.0.1:56556
This is triggered by this method:
private void connectAndInitActiveMQ() throws JMSException{
logger.debug("It's ready to connect to jms service");
if(null != connection){
try{
logger.debug("Closing connection");
connection.close();
}catch(Exception e){
logger.error(e.getMessage(), e);
}
}
logger.debug("Creating a new connection");
logger.debug("Is queueConnectionFactory null? "+(queueConnectionFactory==null));
connection = queueConnectionFactory.createConnection();
logger.debug("Is the new connection null? "+(connection==null));
logger.debug("Starting the new connection");
connection.start();
logger.debug("Connected successfully: " + connection);
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
queue = session.createQueue(queueName);
messageProducer = session.createProducer(queue);
}
Is it the factory problem? Or some other source?
You would get this error if you configured your connections to have the same client ID. The JMS spec is explicit that only a single connection can connect to the remote with the same Client ID at any given time, resolve your configuration and things should work just fine.

using oPort option in Apache commons SFTP

I have a sftp server which i can connect manually using the command below
sftp -oport=4022 user#xxxxxx.com
but I am finding difficulty in doing the same with apache commons vfs.
Below is the method I am using to establish connection to the sftp server. But it not working and fails with the error "org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at xxxxxx.com"
public boolean connect(String host, String login, String password,
int port) throws Exception {
//If the client is already connected, disconnect
if (command != null) {
disconnect();
}
FileSystemOptions fso = new FileSystemOptions();
try {
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fso,
"no");
session =
SftpClientFactory.createConnection(host, port, login.toCharArray(),
password.toCharArray(),
fso);
System.out.println("pass");
Channel channel = session.openChannel("ssh");
channel.connect();
command = (ChannelSftp)channel;
} catch (FileSystemException e) {
throw e;
// return false;
}
return command.isConnected();
}
Please help me with this

SFTP using jschexception verify false sometimes fails

We are getting jcraft.jsch.JSchException: verify: false while getting the ChannelSftp. Now a days it is occurring very frequently.
Below is my environment where I am getting this error.
java version "1.7.0_10" and 1.7.0_13
weblogic 11g(10.3.6.0)
jsch jar version jsch-0.1.50.jar
any suggestions on how to resolve this issue are much helpful.
Here is my block of code that I have used for getting channel
ChannelSftp sftpChannel = null;
JSch jsch = new JSch();
Properties jschConfig = new Properties();
jschConfig.setProperty("compression.s2c", "none");
jschConfig.setProperty("compression.c2s", "none");
jschConfig.setProperty("StrictHostKeyChecking", "no");
jschConfig.setProperty("PreferredAuthentications", "publickey,password");
JSch.setConfig(jschConfig);
Session session = null;
try {
jsch.removeAllIdentity();
jsch.addIdentity(privateKeyFile);
session = jsch.getSession(user, host, port);
session.connect();
} catch (JSchException e) {
throw new RuntimeException("Failed to connect to " + host, e);
}
sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();

RabbitMQ throws AlreadyClosedException rather than IOException when the broker connection is lost

I'm using rabbitmq java client 2.4.1 the newest version.
After a TCP connection lost, and still call a method on a channel over
this connection, a AlreadyClosedException will be thrown.
Is it a bug? I expected an IOException, but AlreadyClosedException I
got, and AlreadyClosedException is a RuntimeException.
If not, why all other errors cause an IOException.
#Test
public void testConnectionLost() throws IOException{
ConnectionFactory factory = new ConnectionFactory();
factory.setRequestedHeartbeat(60);
factory.setHost("<your rabbitmq host>");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
connection.close();
try {
channel.queueDeclare("queueName", false, false, false, null);
Assert.fail("Exception expected.");
}catch (IOException e) {
//it will NOT reach here.
//Inner exception should be AlreadyClosedException
System.out.println(e);
}catch (AlreadyClosedException e) {
// it will reach here.
System.out.println(e);
//this is strange!
//I expected IOException , but AlreadyClosedException I got.
//And AlreadyClosedException is a RuntimeException.
}
Thank you.
If your client loses the TCP connection to your broker, the connection is considered "closed". Therefore it is appropriate (and not a bug) for the client library to throw an AlreadyClosedException.
In other words, a connection is considered "closed" no matter how it got closed (either through a graceful manner or through an unexpected failure).