I started a project in java with ActiveMQ 5.17.0.
Therefore I have downloaded ActiveMQ and opened my terminal from the folder bin and execute ./activemq start.
I received:
INFO: Loading '/Users/NAME/Downloads/apache-activemq-5.17.0//bin/env'
INFO: Using java '/usr/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
./activemq: line 343: [: : integer expression expected
INFO: pidfile created : '/Users/NAME/Downloads/apache-activemq-5.17.0//data/activemq.pid' (pid '3084')
As I have seen in other posts, this is the expected result.
A part of my Java Code is:
public static void main(String[] args) {
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:8161"); // ActiveMQ-specific
Connection con = null;
try {
con = factory.createConnection();
Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE); // non-transacted session
Queue queue = session.createQueue("test.queue"); // only specifies queue name
MessageProducer producer = session.createProducer(queue);
Message msg = session.createTextMessage("hello queue"); // text message
producer.send(msg);
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close(); // free all resources
} catch (JMSException e) { /* Ignore */ }
}
}
}
As soon as I try to open the website https://localhost:8161/admin/admin I receive the response that Safari, Chrome, and Firefox do not get connection to localhost.
Anybody any suggestions what to do?
I have already tried to download again, different browsers, start and stop the server several times.
The URL for the admin UI should be http: http://localhost:8161/admin or http://127.0.0.1:8161/admin/
The script activemq fails parsing the output of java -version. Maybe set the line VERSION= to your java version (e.g. VERSION=17 for java 17) to check if that is the issue.
Your code sample shows that you are attempting to send messages to the Web UI URL vs the JMS protocol port.
Try changing your code to use port '61616':
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616); // ActiveMQ-specific
For the web browser access, try starting with the non-SSL url: http://localhost:8161/admin/admin instead of 'https'.
Related
Im losing my mind, is there any clear documentation how to run java RMI between host and client remotly? i find in different forums, various puzzle pieces, but never, a from a to z guide, which scurity guidelines have to be followed etc, so that a RMI works. Well, if I run the server and client locally, it works.
Now, my low-level problem is that even if I set the external server address everywhere (?), in the error message it says that "localhost" has refused the connection.... Do you have any ideas?
Client:
try {
System.setProperty("java.rmi.server.hostname","42.155.241.914");
String name = "RemoteBookService";
String serverIP = "42.155.241.914"; // or localhost if client and server on same machine.
int serverPort = 1099;
Registry registry = LocateRegistry.getRegistry(serverIP, serverPort);
IRemoteService rs = (IRemoteService) registry.lookup(name);
Error:
java.rmi.ConnectException: Connection refused to host: localhost;
nested exception is:
java.net.ConnectException: Connection refused: connect
Server:
public static void main(String[] args) throws MalformedURLException, AlreadyBoundException {
try {
System.setProperty("java.rmi.server.hostname","42.155.241.914");
LocateRegistry.createRegistry(1099);
String name="//42.155.241.914/RemoteBookService";
RemoteService rs = new RemoteService();
Naming.bind(name, rs);
System.out.println("Service started");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("could not start registry");
}
}
At first glance it looks like your server uses LocateRegistry.createRegistry(port); which implies own registry (same process) which you don't store to local variable, and then you call Naming.rebind(bind, rs) which implies use of a local registry (same host, other process) on the default port.
Try changing the lines on server to:
Registry registry = LocateRegistry.createRegistry(port);
...
// NOTE registry.rebind not called with name="//42.155.241.914/RemoteBookService"
registry.rebind("RemoteBookService", stub);
Hopefully this may get you to the next step, but there are plenty of ways this could have gone wrong so you might need to edit the question with more details.
I'm facing an issue when trying to store a file in an FTP server. When trying to upload a file to the FTP server, the file is created but its contents are not copied.
Context
We use the following configuration to access the FTP server using lftp. I cannot change this configuration, and don't know why do we use FTPS with verify-certificates disabled.
# FTPS_OPTIONS:
set ssl:verify-certificate/testhostname no;
set ftp:ssl-protect-data yes;
set ftp:passive-mode on;
I need to store certain files from a Java application. I'm using apache-commons library. The implemented code looks like this:
#Autowired
public FtpService() {
ftpsClient = new FTPSClient();
ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
}
public void uploadFile(String ftpHost, File tempFile, String destination, String filename)
throws UploadException {
ftpsClient.connect(ftpHost, 990);
ftpsClient.execPBSZ(0);
ftpsClient.execPROT("P");
ftpsClient.enterLocalPassiveMode();
ftpsClient.setKeepAlive(true);
ftpsClient.setControlKeepAliveTimeout(3000);
if(ftpsClient.login("user", "password")) {
try (InputStream fileStream = new FileInputStream(tempFile)) {
if (!ftpsClient.changeWorkingDirectory(destination)) {
throwUploadException("Destination directory not available in FTP server");
}
boolean saved = ftpsClient.storeFile(filename, fileStream);
// Following code is not executed since the exception is thrown in the previous line
if (!saved) {
throwUploadException("Unable to save file in FTP server");
}
log.info("Saved FTP file: {}/{}", destination, filename);
}
catch (UploadException | IOException e)
{
throwUploadException(e.getMessage());
}
finally
{
ftpsClient.disconnect();
if (!tempFile.delete()) {
log.warn("Unable to delete '{}' file", tempFile.getAbsolutePath());
}
}
}
}
Problem
I started with a FTPClient (non FTPSClient) but this way I wasn't able to login.
Currently (FTPSClient), I can:
change the working directory
create directories in the FTP server
I cannot:
storeFile: this method throws the following exception, and creates the file in the FTP server, but this is empty
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
Cause: javax.net.ssl.SSLProtocolException: Software caused connection abort: socket write error
listFiles()/listDirectories(): when executing this command, the obtained list is always empty. The logged user has all the required permissions in the whole FTP server
Following is the FTP's log (note that I have translated the commands to English between parenthesis), corresponding to the code shown before, that raises the exception mentioned before:
er: testhostname:990
USER *******
331 Usuario testuser OK. Clave requerida ( = User testuser OK. Password required)
PASS *******
230 OK. El directorio restringido actual es / ( = OK. The current restricted directory is /)
CWD /test/upload
250 OK. El directorio actual es /test/upload ( = Ok. The current directory is /test/upload)
PASV
227 Entering Passive Mode (<numbers...>)
[Replacing PASV mode reply address <ip_address> with testhostname]
STOR dummyfile.txt
150 Conexi├│n de datos aceptada ( = Data connection accepted)
If there is anything else I can include to improve the description, please let me know. Thanks for your help!
I had a similar problem from python connecting to an FTPS server. The error was that the server required the data channel session to be the same as the control channel session(reuse the session). The solution was to override one of the methods to do that.
You can test extending FTPClient.java and overriding the next method:
#Override
protected void _prepareDataSocket_(final Socket socket) {
if(preferences.getBoolean("ftp.tls.session.requirereuse")) {
if(socket instanceof SSLSocket) {
// Control socket is SSL
final SSLSession session = ((SSLSocket) _socket_).getSession();
if(session.isValid()) {
final SSLSessionContext context = session.getSessionContext();
context.setSessionCacheSize(preferences.getInteger("ftp.ssl.session.cache.size"));
try {
final Field sessionHostPortCache = context.getClass().getDeclaredField("sessionHostPortCache");
sessionHostPortCache.setAccessible(true);
final Object cache = sessionHostPortCache.get(context);
final Method putMethod = cache.getClass().getDeclaredMethod("put", Object.class, Object.class);
putMethod.setAccessible(true);
Method getHostMethod;
try {
getHostMethod = socket.getClass().getMethod("getPeerHost");
}
catch(NoSuchMethodException e) {
// Running in IKVM
getHostMethod = socket.getClass().getDeclaredMethod("getHost");
}
getHostMethod.setAccessible(true);
Object peerHost = getHostMethod.invoke(socket);
putMethod.invoke(cache, String.format("%s:%s", peerHost, socket.getPort()).toLowerCase(Locale.ROOT), session);
}
catch(NoSuchFieldException e) {
// Not running in expected JRE
log.warn("No field sessionHostPortCache in SSLSessionContext", e);
}
catch(Exception e) {
// Not running in expected JRE
log.warn(e.getMessage());
}
}
else {
log.warn(String.format("SSL session %s for socket %s is not rejoinable", session, socket));
}
}
}
}
I found this Java solution here: https://stackoverflow.com/a/32404418/19599290
Getting MQJCA1025 error while starting up the Asynchronous Listener/reader on IBM-MQ as -
com.ibm.msg.client.jms.DetailedIllegalStateException: MQJCA1025: The
message consumer must not have a message listener. An application
attempted to set a message listener for a JMS message consumer. This
exception occurs only if the application is running in a managed
environment. Modify the application so that it does use a message
listener.
Below is the init method where listener setup is done -
public void init(){
ConnectionFactory qConnectionFactory = null;
Connection connection = null;
try{
Context ctx = new InitialContext();
qConnectionFactory = (ConnectionFactory) ctx.lookup("java:jboss/Connection");
Destination receiverQueue = null;
if(null != qConnectionFactory){
connection = qConnectionFactory.createConnection();
if(null !=connection){
receiverQueue = (Destination) ctx.lookup("java:jboss/RESPONSE");
if(null != receiverQueue){
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
if(null != session){
MessageConsumer consumer = session.createConsumer(receiverQueue);
consumer.setMessageListener(this);
connection.start();
}
}
}
}
}
catch(Exception e){
System.out.println(e);
}
}
IBM resolution at http://www-01.ibm.com/support/docview.wss?uid=swg21610734 doesn't mentions the fix to be made at listener/client side
MQJCA1025 error message says don't do the following:
consumer.setMessageListener(this);
The link you give is very clear:
Cause
Calling setMessageListener() method in a managed environment is a violation of J2EE specification, and therefore should not be used.
Resolving the problem
Your JMS application needs to be altered to not call setMessageListener() method. Instead, Activation Specifications are provided for the functionality which this method provides
In other words, your Application Server does the hookup for you :-) Your fix would be to NOT set the listener yourself, but instead make sure you implement a MessageDrivenBean and the application server will do the registration for you. You only need to implement onMessage(). See here for an example with JBoss.
I have wrote following code:
private static void startH2(){
Server server = null;
try {
server = Server.createTcpServer("-tcpAllowOthers").start();
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL", "sa", "");
} catch (Exception e) {
LOG.error("Error while initialize", e);
}
System.out.println("finish");
}
public static void main(String [] args){
startH2();
}
I run my main method and see following situation:
Looks like Server.createTcpServer creates new non daemon thread.
but by url localhost:8082 I don't see h2 web console(actual result - ERR_CONNECTION_REFUSED)
How to fix this?
P.S.
I have noticed that by url
http://localhost:9092/
my browser downlods file with strange content:
if to decode this text I see following message:
Version mismatch, driver version is “0” but server version is “15”
I use h2 version 1.4.182
H2 contains multiple servers:
the TCP Server (for H2 JDBC clients),
the Web Server (for browsers, the H2 Console application), and
the PG Server (for PostgreSQL clients).
You have started the TCP Server. If you want to use a browser, you also need to start the Web Server:
private static void startH2(){
Server tcpServer = null;
Server webServer = null;
try {
tcpServer = Server.createTcpServer("-tcpAllowOthers").start();
System.out.println("TCP Server Port: " + tcpServer.getPort());
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:tcp://localhost/~/test22;MODE=PostgreSQL", "sa", "");
webServer = Server.createWebServer().start();
System.out.println("Web Server (H2Console) Port: " + webServer.getPort());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("finish");
}
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.