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

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

Related

Is it a possible what get a PID of running process from given port?

I'm trying to write a packet capturing java program using the jnetpcap library. But I wonder if I can get the PID from a given port? In my case, I'm trying to use Sigar API to get the PID (method name is getProcPort(protocol, port)).
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>(){
public void nextPacket(PcapPacket packet, String user) {
Tcp tcp = new Tcp();
Ip4 ip = new Ip4();
String protocol;
long port;
if(packet.hasHeader(ip)&&packet.hasHeader(tcp)){
protocol = tcp.getName();
port = tcp.source();
try {
//but the following line will cause an error
long pid = sigar.getProcPort("tcp", Long.toString(port));
System.out.println("pid : " + pid);
} catch (SigarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(filtering==false){
printPacketResult(packet, tcp, ip, port, protocol);
}else if(filtering==true){
filteredPrintPacketResult(packet, tcp, ip, port, protocol);
}
}
}
};
and the error I get is:
org.hyperic.sigar.SigarNotImplementedException: This method has not
been implemented on this platform at
org.hyperic.sigar.SigarNotImplementedException.(SigarNotImplementedException.java:28)
at org.hyperic.sigar.Sigar.getProcPort(Native Method) at
org.hyperic.sigar.Sigar.getProcPort(Sigar.java:632) at
networkInfo.PackageCapture$1.nextPacket(PackageCapture.java:84) at
networkInfo.PackageCapture$1.nextPacket(PackageCapture.java:1) at
org.jnetpcap.Pcap.loop(Native Method) at
org.jnetpcap.Pcap.loop(Unknown Source) at
networkInfo.PackageCapture.startNet(PackageCapture.java:111) at
networkInfo.FilterChoice.choice(FilterChoice.java:27) at
networkInfo.Main.main(Main.java:6)
Is it a possible? If you have some information, please give me some hint.

cassandra single node connection error

i am trying to use cassandra as database for an app i am working on. The app is a Netbeans platform app.
In order to start the cassandra server on my localhost i issue Runtime.getRuntime().exec(command)
where command is the string to start the cassandra server and then i connect to the cassandra sever with the datastax driver. However i get the error:
com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1:9042 (com.datastax.driver.core.TransportException: [/127.0.0.1:9042] Cannot connect))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:199)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:80)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1154)
at com.datastax.driver.core.Cluster.getMetadata(Cluster.java:318)
at org.dhviz.boot.DatabaseClient.connect(DatabaseClient.java:43)
at org.dhviz.boot.Installer.restored(Installer.java:67)
....
i figure it out that the server requires some time to start so i have added the line Thread.sleep(MAX_DELAY_SERVER) which seem to resolve the problem.
Is there any more elegant way to sort this issue?
Thanks.
Code is below.
public class Installer extends ModuleInstall {
private final int MAX_DELAY_SERVER = 12000;
//private static final String pathSrc = "/org/dhviz/resources";
#Override
public void restored() {
/*
-*-*-*-*-*DESCRIPTION*-*-*-*-*-*
IMPLEMENT THE CASSANDRA DATABASE
*********************************
*/
DatabaseClient d = new DatabaseClient();
// launch an instance of the cassandra server
d.loadDatabaseServer();
/*wait for MAX_DELAY_SERVER milliseconds before launching the other instructions.
*/
try {
Thread.sleep(MAX_DELAY_SERVER);
Logger.getLogger(Installer.class.getName()).log(Level.INFO, "wait for MAX_DELAY_SERVER milliseconds before the connect database");
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
Logger.getLogger(Installer.class.getName()).log(Level.INFO, "exeption in thread sleep");
}
d.connect("127.0.0.1");
}
}
public class DatabaseClient {
private Cluster cluster;
private Session session;
private ShellCommand shellCommand;
private final String defaultKeyspace = "dhviz";
final private String LOAD_CASSANDRA = "launchctl load /usr/local/Cellar/cassandra/2.1.2/homebrew.mxcl.cassandra.plist";
final private String UNLOAD_CASSANDRA = "launchctl unload /usr/local/Cellar/cassandra/2.1.2/homebrew.mxcl.cassandra.plist";
public DatabaseClient() {
shellCommand = new ShellCommand();
}
public void connect(String node) {
//this connect to the cassandra database
cluster = Cluster.builder()
.addContactPoint(node).build();
// cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis(12000);
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n",
metadata.getClusterName());
for (Host host
: metadata.getAllHosts()) {
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
Logger.getLogger(DatabaseClient.class.getName()).log(Level.INFO, "connected to server");
}
public void loadDatabaseServer() {
if (shellCommand == null) {
shellCommand = new ShellCommand();
}
shellCommand.executeCommand(LOAD_CASSANDRA);
Logger.getLogger(DatabaseClient.class.getName()).log(Level.INFO, "database cassandra loaded");
}
public void unloadDatabaseServer() {
if (shellCommand == null) {
shellCommand = new ShellCommand();
}
shellCommand.executeCommand(UNLOAD_CASSANDRA);
Logger.getLogger(DatabaseClient.class.getName()).log(Level.INFO, "database cassandra unloaded");
}
}
If you are calling cassandra without any parameters in Runtime.getRuntime().exec(command) it's likely that this is spawning cassandra as a background process and returning before the cassandra node has fully started and is listening.
I'm not sure why you are attempting to embed cassandra in your app, but you may find using cassandra-unit useful for providing a mechanism to embed cassandra in your app. It's primarily used for running tests that require a cassandra instance, but it may also meet your use case.
The wiki provides a helpful example on how to start an embedded cassandra instance using cassandra-unit:
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
In my experience cassandra-unit will wait until the server is up and listening before returning. You could also write a method that waits until a socket is in use, using logic opposite of this answer.
I have changed the code to the following taking inspiration from the answers below. Thanks for your help!
cluster = Cluster.builder()
.addContactPoint(node).build();
cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis(50000);
boolean serverConnected = false;
while (serverConnected == false) {
try {
try {
Thread.sleep(MAX_DELAY_SERVER);
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
}
cluster = Cluster.builder()
.addContactPoint(node).build();
cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis(50000);
session = cluster.connect();
serverConnected = true;
} catch (NoHostAvailableException ex) {
Logger.getLogger(DatabaseClient.class.getName()).log(Level.INFO, "trying connection to cassandra server...");
serverConnected = false;
}
}

ActiveMQ KahaDB always locking and waiting

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

RMI - work in the same machine, not in LAN

I am trying to establish two-communication between one server and two clients. This works very well when all programs run on the same machine but it doesn't work when I try using LAN network.
I got the error :
java.rmi.ConnectException: Connection refused to host: 192.168.1.24; nested exception is:
java.net.ConnectException: Connection timed out: connect
Here is the server code :
public class Server{
private Game partie; // The class Game extends UnicastRemoteObject and implements ServerInterface
public Server() throws RemoteException {
System.setProperty("java.rmi.server.hostname", "192.168.1.24");
partie = new Game();
LocateRegistry.createRegistry(1099);
try{
Naming.rebind("Server", partie);
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String argv[]) throws RemoteException{
new Server();
}
}
Here is the constructor of the client code :
public Client(String aName, String aServerAdress) throws RemoteException {
super();
name = aName;
ServerAdress = aServerAdress; // = "192.168.1.24"
theRegistry = LocateRegistry.getRegistry(ServerAdress);
try {
serverInterface = (ServerInterface) theRegistry.lookup("Server");
} catch (NotBoundException e1) {
e1.printStackTrace();
}
try {
theRegistry.bind(name, this); // For two-way communication
} catch (AlreadyBoundException e) {
e.printStackTrace();
}
serverInterface.registerClient(name);
}
Where registerClient(String name) code is approximately (in Game class) :
cd_client = (ClientInterface) Naming.lookup("rmi://127.0.0.1/" + name);
All firewalls are disabled.
I have been working on this problems for many hours and I have still not found what is wrong. I would really appreciate if you could help me a bit.
Thank you
Change all occurances of 127.0.0.1 (except registry binding) to your LAN IP address (192.168.1.24 in your case)
127.0.0.1 is a Loopback address:
"Loopback (loop-back) describes ways of routing electronic signals,
digital data streams, or flows of items from their originating
facility back to the receiving end of the source without intentional
processing or modification. This is primarily a means of testing the
transmission or transportation infrastructure."
-- from Wikipedia

Java RMI Client access denied

I have a small Java RMI Server and Client program I'm writing. I have spent some time trying to figure out the error messages without success.
The Client generates the following error:
Trying to connect to: 127.0.0.1:3232
ERROR!!!: StockClient: main: Could not connect to the server: java.rmi.UnmarshalException: Error unmarshaling return header; nested
exception is:
java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.EOFException
java.io.EOFException
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:209)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at StockClient.StockClient.main(StockClient.java:44)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:250)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:195)
... 3 more
With the server the following error only when the client attempts to connect.
this address=localhost/127.0.0.1,port=3232
Exception in thread "RMI TCP Connection(idle)" java.security.AccessControlException: access denied
(java.net.SocketPermission 127.0.0.1:62586 accept,resolve)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:549)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkAccept(SecurityManager.java:1157)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.checkAcceptPermission(TCPTransport.java:636)
at sun.rmi.transport.tcp.TCPTransport.checkAcceptPermission(TCPTransport.java:275)
at sun.rmi.transport.Transport$1.run(Transport.java:158)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Because of the server error I'm fairly sure its a security or registry error, security policy for the server is:
grant {
permission java.security.AllPermission;
};
and being run with the following argument
-Djava.security.policy=client.policy
I've tried everything I can find but seem to keep going around in circles.
relevant methods:
Server:
public static void main(String[] args)
{//main(...) starts
// set up the data structures and data
//add users
//Users hard coded as this is an example proof on concept program
//Names and passwords not hashed for simplicity
User alpha = new User("Alpha", "AlphaPass");
User omega = new User("Omega", "OmegaPass");
users.add(alpha);
users.add(omega);
//startup the RMI server
try
{
System.setSecurityManager(new RMISecurityManager());
StockServer server = new StockServer();
StockServerInterface inter = (StockServerInterface)
UnicastRemoteObject.exportObject (server,0);
// create the registry and bind the name and object.
registry = LocateRegistry.createRegistry(thisPort);
registry.rebind("StockServer", inter);
}
catch (Exception e)
{
System.out.println("Unable to create StockServer: " + e);
e.printStackTrace();
System.exit(1);
}
}//main(...) ends
/**
* Constructor for StockServer
*
* #throws RemoteException
*/
public StockServer() throws RemoteException
{
//try to get the host machine's IP address
try
{
// get the address of this host.
thisAddress = (InetAddress.getLocalHost()).toString();
} catch (Exception e)
{
throw new RemoteException("can't get inet address. " + e);
}
//Set the port
thisPort = 3232;
//Print out the server address and port
System.out.println("this address=" + thisAddress + ",port=" + thisPort);
}
Client:
private static StockServerInterface stockServer;
static public void main(String args[])
{
Registry registry;
//get the server address from the args
String serverAddress = args[0];
//get the server port from the args
String serverPort = args[1];
//Let the user know we are about to try to connect
System.out.println("Trying to connect to: " + serverAddress + ":" + serverPort);
try
{
// get the registry
registry = LocateRegistry.getRegistry(
serverAddress,
(new Integer(serverPort)).intValue());
// look up the remote object
stockServer = (StockServerInterface) (registry.lookup("StockServer"));
//Authenticate the user
authenticate();
//setup the hashset
HashSet<Stock> hashStockSet = null;
//setup the hashset of desired stocks
try
{
hashStockSet = getHashSet();
} catch (IOException e)
{
e.printStackTrace();
System.exit(1);
} catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
//bit of a heavy handed infinte loop so we continue to get the loop
while(true)
{
//Run the ticker
ticker(hashStockSet);
}
// call the remote method
}
catch (RemoteException e)
{
System.out.println("ERROR!!!: StockClient: main: Could not connect to the server: "+e);
e.printStackTrace();
}
catch (NotBoundException e)
{
System.out.println("ERROR!!!: StockClient: main: Could not connect to the server: "+e);
e.printStackTrace();
}
You don't need a SecurityManager in an RMI server unless the client is relying on the codebase feature to supply the server with classes. Either remove it, or debug the .policy file. Clearly the one you've written isn't being loaded.
Run your server with -Djava.security.debug=access,failure and you will see where all the security domains are getting their configurations from, and the domain that is failing at the point where the exception is thrown.

Categories

Resources