Export rmi on virtual machine - java

I want to export java.rmi on virtual machine,
<bean id="entityRmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="entityService"/>
<property name="service" ref="entityServiceImpl"/>
<property name="serviceInterface" value="IEntityService"/>
<property name="registryPort" value="1099"/>
<property name="registryHost" value="127.0.0.1"/>
i get connection refused to 127.0.0.1
hosts file :
127.0.0.1 localhost.localdomain localhost
10.0.2.15 compname
is this problem with vm ?

Change the networking mode of the virtual machine to "bridged" so it will be in the same network as the host. Java RMI was not designed to work with NAT, so you'll have trouble with the default networking mode.
After that, change registryHost to the IP address of the VM, 127.0.0.1 can only be accessed from within the machine.
(There are other ways to make this work but this is the easiest to explain)

Related

Can't connect to Apache Ignite Cluster on AWS EC2 - Ignite server rejects clients

I have problems with connecting to my Ignite cluster. I have 2 nodes on AWS EC2 and I use AWS S3 based discovery. Ignites nodes start without errors and I see how new objects appear at S3 bucket after launching Ignite.
In AWS Securuty groups I opened all TCP ports so that it is expected that anyone can connect to the cluster.
But I get org.apache.ignite.client.ClientConnectionException: Ignite cluster is unavailable exception when I try to connect from my computer (I try to run simple JUnit tests from IDE).
I used the following configuration:
<property name="addressResolver">
<bean class="org.apache.ignite.configuration.BasicAddressResolver">
<constructor-arg>
<map>
<entry key="EC2 internal IP" value="EC2 public ip"/>
</map>
</constructor-arg>
</bean>
</property>
<property name="discoverySpi">
<bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
<property name="ipFinder">
<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.s3.TcpDiscoveryS3IpFinder">
<property name="awsCredentials" ref="reference to another bean with aws creds"/>
<property name="bucketName" value="MY_BUCKET_NAME"/>
</bean>
</property>
</bean>
</property>
The problem here is that server rejects any attempts to connect, because when I start Ignite client (not Thin client) I see at logs the following picture:
As you can see, the number of clients become equal to 0 then 1, 0 and 1 and etc.
So I suppose, clients can reach the cluster, but why are they being rejected?
I solved this problem. It seems there is a rule: If you use AWS S3 based discovery in your Ignite cluster, your Ignite client nodes should use the same approach as well. It wasn't obvious for me.
According to the official documentation:
TcpDiscoverySpi spi = new TcpDiscoverySpi();
BasicAWSCredentials creds = new BasicAWSCredentials("yourAccessKey", "yourSecreteKey");
TcpDiscoveryS3IpFinder ipFinder = new TcpDiscoveryS3IpFinder();
ipFinder.setAwsCredentials(creds);
ipFinder.setBucketName("yourBucketName");

cannot achieve connectivity with MySQL db on remote machine

I'm developing a Java/Spring/Hibernate/CXF/MySQL SOAP webservice and corresponding web Spring MVC client on my laptop. The plan is to eventually move the two resulting war-files over to my remote server hosted at mybiz.com .
Both laptop and server have an instance of MySQL version 5.x.x
Both laptop and server have a root#localhost user (duh)
The server also has three other users:
zzdb_admin#%
zzdb_admin#localhost
zzdb_admin#mybiz.com
all with the same password remotepw and which have all had assorted privileges granted and flushed.
Both instances of MySQL have a database named zzdb.
Both instances of MySQL have ##session.old_passwords, ##global.old_passwords and ##global.secure_auth set to 0; In all cases hashes of passwords are 41 characters wide.
While logged in to the remote server directly I can manually log in to all accounts on both machines
mysql --user=root --password=remoterootpw
mysql --user=zzdb_admin --password=remotepw
mysql --host=localhost --user=zzdb_admin --password=remotepw
On the laptop I can log in to the local mysql with
mysql --user=root --password=localrootpw
mysql --host=mybiz.com --user=zzdb_admin --password=remotepw
So all users and passwords are correct. And their hashes are all 41 characters. Important: note this last proves that connection can be made with the instance on the remote machine.
The webservice' pom has version 5.1.8 of mysql-connector-java.
Now it gets weird. With these lines in the webservice' properties file:
hibernate.connection.url=jdbc:mysql://localhost:3306/zzdb
hibernate.connection.username=root
hibernate.connection.password=localrootpw
the webapp can connect to the local db instance and all is peachy. But changing only these three lines to
hibernate.connection.url=jdbc:mysql://mybiz.com:3306/zzdb
hibernate.connection.username=zzdb_admin
hibernate.connection.password=remotepw
throws the dreaded "Access denied for user 'zzdb_admin'#'localhost' " error
This has got me pulling out what few hairs I have left. Doesn't look like I'm missing anything and everything is spelled correctly. Anybody have an idea of what's going on?
TIA,
Still-learning Steve
Addendum: trying a different, simpler approach WORKED!
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://mybiz.com/zzdb?user=zzdb_admin&password=remoteapw";
Connection conn = DriverManager.getConnection(connectionUrl);
Now how about that? Only change is the method of connecting. Crazy
MySQL remote link have a bind address. You should comment it. You can refer this blog.
Solved the problem, and oh what a subtle bugger it was!
The applicationContext.xml at the base of the web service uses an Atomikos bean for a data source
<bean id="dataSourceServerA" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="XADBMS_A" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties"> <!-- properties will be different for different data sources ie MySQL/HSDB/DB2 etc -->
<props>
<prop key="uri">$dbServerA{hibernate.connection.uri}</prop>
<prop key="user">$dbServerA{hibernate.connection.username}</prop>
<prop key="password">$dbServerA{hibernate.connection.password}</prop>
</props>
</property>
<property name="poolSize"><value>20</value></property>
<property name="testQuery" value="SELECT 1" />
which in turn gets used in the EntityManagerFactor bean. This configuration works great as long as I'm pointing to the local instance of MySQL. Changing the configuration to point to the remote instance kept failing with references to zzdb_admin#localhost no matter what I did.
So I created a second, much simpler connectivity tester using only
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://mybiz.com/zzdb?user=zzdb_admin&password=removepw";
theApp.conn = DriverManager.getConnection(connectionUrl);
and EUREKA! It worked to make a remote connection. So that got me thinking the problem had to lie with the Atomikos bean configuration.
Turns out there's one crucial xaProperty I wasn't setting - serverName, which when not explictly set defaults to - you guessed it - localhost. Atomikos isn't "smart" enough to infer the hostname out of the uri passed to it.
So merely switching to
<bean id="dataSourceServerA" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="XADBMS_A" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties"> <!-- properties will be different for different data sources ie MySQL/HSDB/DB2 etc -->
<props>
<prop key="serverName">mybiz.com</prop>
<prop key="port">3306</prop>
<prop key="databaseName">zzdb</prop>
<prop key="user">$dbServerA{hibernate.connection.username}</prop>
<prop key="password">$dbServerA{hibernate.connection.password}</prop>
</props>
</property>
<property name="poolSize"><value>20</value></property>
<property name="testQuery" value="SELECT 1" />
did the trick. So the problem was one of configuration. Fiendishly subtle indeed!
Thanks to all who replied!
CASE CLOSED!
Still-learning Steve

Mail Server with javax.mail and CentOS

I have a Java Program that was installed on an old Ubuntu machine and sent mail using javax.mail. However, that machine went down and I am now running the same Java app in a new CentOS machine.
However, I get an error "MessagingException: 501 Syntax: HELO hostname" when trying to send an email using mail.smtp.host = 127.0.0.1.
My guess is that the mailserver is not yet activated in this CentOS.
How would I go about configuring a mail server that javax.mail can use?
Thank you
Your machine host name must be mapped in /etc/hosts file.
Example:
Console shows: linux#
and cat /etc/hostname shows
linux.mydomain.com
Then edit your hosts file running as root . vi /etc/hosts
127.0.0.1 localhost linux linux.mydomain.com
Good detailed info can be found here: https://confluence.atlassian.com/display/CONFKB/Sending+Email+Fails+Due+to+501+Syntax%3A+HELO+Hostname+Error
I encountered the same issue "MessagingException: 501 Syntax: HELO hostname" when sending out email with Spring MailSender. What works for me is to add in extra property "mail.smtp.localhost" under javaMailProperties like below:
<!-- JAVA MAIL -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="" />
<property name="port" value="25" />
<property name="protocol" value="smtp" />
<property name="username" value="" />
<property name="password" value="" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">true</prop>
<prop key="mail.smtps.starttls.enable">true</prop>
<prop key="mail.smtps.debug">true</prop>
<prop key="mail.smtp.localhost">localhost</prop>
</props>
</property>
</bean>
The problem is that the naming service on the new machine is not properly configured and Java can't find the host name of the machine. The SMTP HELO command needs to include the host name. The server is complaining because it's missing. Turn on JavaMail Session debugging and you can see the actual command this is sent. You can work around this host configuration problem by setting the JavaMail property mail.smtp.localhost to be the host name you want to use in the HELO command.
You need to run sendmail. See here for more info. Configuring sendmail can be a chore and you may want to take the configuration sendmail.cf from the old machine if possible.
I suspect (also) that you should have some central MTA (mail transport agent) set up, such that all machines in your enterprise use this, rather than running one per host. i.e. not using localhost.
In my case etc/hostname was susetest(and not linux.company.com)
Modified etc/hosts from 127.0.0.1 localhost to 127.0.0.1 localhost susetest(also after localhost make sure to use tab, when modifying the file)
Make sure to save changes, postfix stop, postfix start to restart SMTP server.
Should work fine.
(adding properties.setProperty("mail.smtp.localhost", "ourcompany.com"); to the properties also solved the problem, but workaround should not be the fix, when you can find the root cause, even if it takes days in my case)

Making a ms sql connection in persistence.xml in JPA

We need to make a connection to ms sql server from java persistence unit 1.0. I hace following code for oracle database.
<properties>
<property name="toplink.jdbc.url" value="jdbc:oracle:thin:#IP:PORT"/>
<property name="toplink.jdbc.user" value="####"/>
<property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="toplink.jdbc.password" value="####"/>
<property name="toplink.ddl-generation" value="create-tables"/>
<property name="toplink.jdbc.read-connections.max" value="1"/>
<property name="toplink.jdbc.read-connections.min" value="1"/>
<property name="toplink.jdbc.write-connections.max" value="1"/>
<property name="toplink.jdbc.write-connections.min" value="1"/>
<property name="toplink.logging.level" value="SEVERE" />
</properties>
I need the changes that I have to make in the previous code for making a connection to MS Sql Server.
Finally I got the solution.....
Steps to make connection to ms sql from JPA persistence.xml are:
Download the jar files from http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=21599
I chose sql server 2005 so I used sqljdbc4 jar file from the above link.
Change driver name to com.microsoft.sqlserver.jdbc.SQLServerDriver in above given xml file.
Provide necessary username and password at corresponding position in xml file.
In connection url, write jdbc:sqlserver://localhost:port;databaseName=<Database>
Microsoft SQL Server connection can be done in few ways. To use windows authentication, you need to place a dll file in your System 32 directory. After that, you can replace the
connection URL, and user credentials as required. You may need to configure your SQL server
by SQL Server Surface Configuration Manager to allow remote connections and connections through TCP IP.
After that you may try to connect through a plain java class. And after that connect using a persistence unit (in EJB?).
When you download the SQL Server - JDBC Connector ZIP file, you can find a HTML Documentation,
which you must read (it will take 20 minutes). It was a 2 day struggle for me to connect to SQL Server from JDBC.

jdbc4 CommunicationsException

I have a machine running a java app talking to a mysql instance running on the same instance. the app
uses jdbc4 drivers from mysql. I keep getting com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
at random times.
Here is the whole message.
Could not open JDBC Connection for transaction; nested exception is
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was25899 milliseconds ago.The last packet sent successfully to the server was 25899 milliseconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
For mysql, the value of global 'wait_timeout' and 'interactive_timeout' is set to 3600 seconds and 'connect_timeout' is set to 60 secs. the wait timeout value is much higher than the 26 secs(25899 msecs). mentioned in the exception trace.
I use dbcp for connection pooling and here is spring bean config for the datasource.
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx" />
<property name="poolPreparedStatements" value="false" />
<property name="maxActive" value="3" />
<property name="maxIdle" value="3" />
</bean>
Any idea why this could be happening? Will using c3p0 solve the problem ?
Try setting up the Apache Commons DBCP correctly.
You need to set:
validationQuery to SELECT 1+1
testOnBorrow to true
That should fix the problem.
Can you describe how your app is handling connection pooling? I doubt that autoReconnect=true in the JDBC driver would re-pool connections from your app. The app needs to reconnect when it loses a connection.
I'd follow the advice in the exception. You should consider either:
expiring and/or testing connection validity before use in your application,
increasing the server configured values for client timeouts, or
using the Connector/J connection property 'autoReconnect=true' to avoid this problem. Try adding that to your connection URL (consult the docs for the exact syntax) and see if it helps.
I doubt that C3P0 will be that much better than the DBCP that you're already using. The exception is giving you some specific advice. You've tried #3. What about the other two?
I know how to ask WebLogic to check connections before using them. You should find out how to do the same with Tomcat.
I have seen before that Windows machines which have been moved on the network have had trouble with connecting to themselves.
Is there any connectivity problems outside the JVM - i.e. mysql client connecting to the server, and timing out, etc?

Categories

Resources