RMI server to judge the host name of client which called it - java

I have the below program of Spring RMI in which the client send its port no which i s received at the Server end and is being displayed , I am using spring rmi to achieve that now i want is there any way since i want to remove the hard coding that is client would notsent it hostname now simply client would call the servre RMI server and RMI server should be smart enough to judge that the client host name , please advise f in the below program i stop passing the hostname from client side then how would be the server would be smart enough to judge the client hostname itself
calculation interface there should be a method that takes a String as a parameter.
public interface Calculation {
void print(String text );
void registerIpAddress(String ip);
}
public class CalculationImpl implements Calculation {
#Override
void print(String text) {
System.out.println(text);
}
#Override
void registerIpAddress(String ip){
List.add(ip);//or whatever you want
}
your xml files for both client and host are right ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="calculationBean" class="com.javatpoint.CalculationImpl"></bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="calculationBean"></property>
<property name="serviceInterface" value="com.javatpoint.Calculation"></property>
<property name="serviceName" value="CalculationService"></property>
<property name="replaceExistingBinding" value="true"></property>
<property name="registryPort" value="1099"></property>
</bean>
</beans>
client-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="calculationBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/CalculationService"></property>
<property name="serviceInterface" value="com.javatpoint.Calculation"></property>
</bean>
</beans>
create a new instance of applicaionContext in order for this to work
class server {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
need to run your client
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
Calculation calculation = (Calculation) context.getBean("calculationBean");
calculation.print("Copied&Pasted!");
try {
calculation.registerIpAddress(InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
upon googling i can see the method
public static String getClientHost()
throws ServerNotActiveException
now for this i need to extend remote server class can't i achieve same thing in spring rmi itself

Related

How to read application.properties in Java code

Hi I would like to read values from application.properties. I have found that this should work:
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
Environment environment = applicationContext.getEnvironment();
System.out.println("STARTING:");
System.out.println("Connecting to: "+environment.getProperty("datasource"));
}
but I am getting null for datasource value.
my application-context.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:application.properties" />
</beans>
could someone tell me where could be problem?
Please follow this blog.
https://zetcode.com/spring/propertyplaceholder/
This will explain to you how you can to use the application.properties values.
Please Map your been with "application.properties" values
<bean id="dataSource" class="org.springframework.jdbc.datasource.
SimpleDriverDataSource">
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>
and Use
public static void main(String[] args) {
var ctx = new GenericXmlApplicationContext("my-beans.xml");
var dataSource = (SimpleDriverDataSource) ctx.getBean("dataSource");
logger.info("Url: {}", dataSource.getUrl());
logger.info("User name: {}", dataSource.getUsername());
logger.info("Password: {}", dataSource.getPassword());
ctx.close();
}

sending parameter from client to server through RMI

I have to pass a parameter to from client to server through spring RMI please advise how to achieve this below is the class i have editied
package com.javatpoint;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.remoting.support.RemoteInvocation;
#SuppressWarnings("serial")
public class CalculationImpl extends RemoteInvocation implements Calculation {
public int cube(int number) {
return number*number*number;
}
public CalculationImpl (MethodInvocation methodInvocation) {
super();
// Invoked in superclass
this.addAttribute("awer", "test1111");
}
private void addAttribute(String string, String string2) {
}
}
and the xml for the client is
<beans>
<bean id="calculationBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="remoteInvocationFactory" ref="invocationFactory"/>
<property name="serviceUrl" value="rmi://localhost:1099/CalculationService"></property>
<property name="serviceInterface" value="com.javatpoint.Calculation"></property>
</bean>
<bean id="invocationFactory" class="com.javatpoint.CalculationImpl"/>
</beans>
now please advise how to customise the rmiservice exporter so that it should recieve the value sent fromt he client lets say as shown above in client xml the value of parameter awer is test1111 now i want to customise my rmiservice exporter
so that it should recieve this value and display it
below is my rmi service exporter..
<bean id="calculationBean" class="com.javatpoint.CalculationImpl"></bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="calculationBean"></property>
<property name="serviceInterface" value="com.javatpoint.Calculation"></property>
<property name="serviceName" value="CalculationService"></property>
<property name="registryPort" value="1099"></property>
</bean>
In your calculation interface there should be a method that takes a String as a parameter.
public interface Calculation {
void print(String text );
void registerIpAddress(String ip);
}
public class CalculationImpl implements Calculation {
#Override
void print(String text) {
System.out.println(text);
}
#Override
void registerIpAddress(String ip){
List.add(ip);//or whatever you want
}
your xml files for both client and host are right, but you are missing a few things.
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="calculationBean" class="com.javatpoint.CalculationImpl"></bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="calculationBean"></property>
<property name="serviceInterface" value="com.javatpoint.Calculation"></property>
<property name="serviceName" value="CalculationService"></property>
<property name="replaceExistingBinding" value="true"></property>
<property name="registryPort" value="1099"></property>
</bean>
</beans>
client-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="calculationBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/CalculationService"></property>
<property name="serviceInterface" value="com.javatpoint.Calculation"></property>
</bean>
</beans>
you need to create a new instance of applicaionContext in order for this to work
class server {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
finally you need to run your client
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
Calculation calculation = (Calculation) context.getBean("calculationBean");
calculation.print("Copied&Pasted!");
try {
calculation.registerIpAddress(InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

SessionFactory not initialized

i created a DAO (ForecastPersistorDao.java) which is the interface and its corresponding implementation (ForecastPersistorDaoImpl.java). The implementation has a method 'persist()' whose job is to persist some data into the database. When I try to call persist to persist the data, it throws a NullPointerException since it does not initialize SessionFactory properly. Please help me point out what I might be doing wrong:
ForecastPersistorDao.java
public interface ForecastPersistorDao {
void persist(List<ForecastedDemand> forecastedDemands);
List<DemandForecast> retrieveLastForecast(String marketplaceId);
}
ForecastPersistorDaoImpl.java
#Repository("forecastPersistorDao")
public class ForecastPersistorDaoImpl implements ForecastPersistorDao {
private SessionFactory sessionFactory;
#Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* Persist forecast in the database for later
*
* #param forecastedDemands
* List of forecast for all asin:marketplaceId tuple
*/
#Transactional
#Override
public void persist(List<ForecastedDemand> forecastedDemands) {
System.out.println("THIS IS ALWAYS NULL-------->>>>>>>> " + sessionFactory);
Session session = sessionFactory.getCurrentSession();
Date forecastCalculationDate = new Date();
// For each [asin:marketplace] tuple
for (ForecastedDemand forecastedDemand : forecastedDemands) {
String asin = forecastedDemand.getAsinMarketplaceId().getAsin();
String marketplaceId = forecastedDemand.getAsinMarketplaceId().getMarketplaceId();
String forecastingModel = forecastedDemand.getForecastingModel();
SortedMap<Instant, Double> forecast = forecastedDemand.getForecast();
// for each forecast date - write an entry in demand_forecasts table
for (Map.Entry<Instant, Double> entry : forecast.entrySet()) {
Date dateOfForecast = entry.getKey().toDate();
double quantityForecasted = entry.getValue();
DemandForecast forecastToPersist = new DemandForecast(asin, marketplaceId, forecastCalculationDate,
forecastingModel, quantityForecasted, dateOfForecast);
session.save(forecastToPersist);
}
}
}
Main class (Runner.java):
public final class FbsRunner {
private ForecastPersistorDao forecastPersistorDao;
public static void main(String[] args) {
Runner runner = new Runner();
runner.run();
}
public void run() {
ApplicationContext context =
new FileSystemXmlApplicationContext("spring-configuration/application-config.xml");
forecastPersistorDao = (ForecastPersistorDao) context.getBean("forecastPersistorDao"); // this works fine
System.out.println(">>>>>>>>>> forecastPersistorDao [this is fine (not null)]: " + forecastPersistorDao);
List<ForecastedDemand> forecastedDemand = [a list of Forecasted demand to be persisted int he DB]
// THE CALL BELOW FAILS...
forecastPersistorDao.persist(forecastedDemands);
System.out.println("Persisted demand in the database"); // We don't reach here.
}
}
spring-configuration/application-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<!-- The main application context spring configuration -->
<import resource="application/hibernate.xml" />
<import resource="common/hibernate.xml" />
<!--<import resource="application/proxies.xml" />-->
<!--<import resource="common/aggregators.xml" /> -->
<import resource="application/environment.xml" />
<!--
Add any beans specific to your application here
-->
<bean id="forecastPersistorDao" class="com.amazon.fresh.fbs.dao.ForecastPersistorDaoImpl" />
</beans>
application/hibernate.xml:
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
parent="AbstractSessionFactory" depends-on="EnvironmentHelper" >
<property name="hibernateProperties">
...
...
everything as expected
...
</bean>
common/hibernate.xml:
<beans ... >
bean id="AbstractSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
abstract="true">
<property name="mappingResources">
<list>
<value>com/amazon/fresh/fbs/dao/hibernate/RtipData.hbm.xml</value>
<value>com/amazon/fresh/fbs/dao/hibernate/Vendor.hbm.xml</value>
<value>com/amazon/fresh/fbs/dao/hibernate/AdjustmentPeriod.hbm.xml</value>
<value>com/amazon/fresh/fbs/dao/hibernate/DemandForecast.hbm.xml</value>
</list>
</property>
<property name="exposeTransactionAwareSessionFactory">
<value>true</value>
</property>
</bean>
<!-- Use Spring transactions for Hibernate -->
<tx:annotation-driven transaction-manager="txManager" mode='proxy' proxy-target-class='true'/>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<aop:aspectj-autoproxy/>
</beans>
Please try to add <context:component-scan base-package="YOUR PACKAGE NAME" /> into your application-config.xml. This will tell Spring to scan for annotated components that will be auto-registered as Spring beans.
"YOUR PACKAGE NAME" should be the package name to scan for annotated components.
sessionFactory didn't autowired correctly ,add "#component" for ForecastPersistorDaoImpl and add "context:component-scan" in application-config.xml to tell spring to initialize.

Object retrieved from ioc turned null after JMX method invoker

I am using Spring 3.1 as standalone app.
I have some wierd situation and I guess I am missing something.
I have class which has method that I have exposed via JMX console.
that method is invoking a method via bean.
that bean I am initializing in advaned.
the wierd thing that after i am invoking the method via the jmx console the bean instance turn to be null.
Thats the JMX bean which is exposed:
public class TriggerBean implements IJmxTriggerBean
{
static Logger logger = Logger.getLogger(TriggerBean.class);
FeedListenerBean fe = null;
public void start()
{
try
{
// init();
PropertyConfigurator.configure(FixGWConstants.LOG4J_PATH);
ApplicationContext context = new ClassPathXmlApplicationContext(FixGWConstants.APPLICATION_CONTEXT_XML);
fe = (FeedListenerBean) context.getBean("FeedListenerBean");
doTheListen();
}
catch (Throwable t)
{
logger.error(t);
System.out.println(t);
}
}
//thats the method being exposed by jmx. pay attention that fe object has been initialized before by the start()
public void doTheListen()
{
fe.listen();
}
private void init()
{
Resource resource = new ClassPathResource(System.getProperty("user.dir") + "//config.properties");
try
{
Properties props = PropertiesLoaderUtils.loadProperties(resource);
String log4jPath = props.getProperty("LOG4J_PATH");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I am testing it via standalone:
public static void main(String[] args) throws Exception
{
protected TriggerBean trigger = new Trigger();
trigger.start();
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- Must for auto wiring
<context:annotation-config />
-->
<context:component-scan base-package="com.fixgw.beans">
</context:component-scan>
<!-- start a JMX Server -->
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean" />
<bean id="FeedListenerBean" class="com.fixgw.beans.FeedListenerBean">
</bean>
<bean id="TriggerBean" class="com.fixgw.test.TriggerBean">
</bean>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="Server:name=HttpAdaptor">
<bean class="mx4j.tools.adaptor.http.HttpAdaptor">
<property name="port" value="8000" />
<property name="host" value="0.0.0.0" />
<property name="processor">
<bean class="mx4j.tools.adaptor.http.XSLTProcessor" />
</property>
</bean>
</entry>
<entry key="bean:name=TriggerBean" value-ref="TriggerBean" />
</map>
</property>
<property name="listeners">
<list>
<!--
let the HttpAdapter be started after it is registered in the
MBeanServer
-->
<bean class="com.fixgw.jmx.HttpAdaptorMgr">
<property name="mbeanServer" ref="mbeanServer" />
</bean>
</list>
</property>
</bean>
</beans>
When I first using start() the object fe is doing great.
but after invoking doTheListen( via JMX the object fe remind null(although it has already been initialized before)
any idea?
thanks,
ray.

Configure and connect to Spring JMX

I am trying to configure JMX console for my standalone Spring application.
I have configured it this way:
Application context:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- Must for auto wiring
<context:annotation-config />
-->
<context:component-scan base-package="com.fixgw.beans">
</context:component-scan>
<bean id="FeedListenerBean" class="com.fixgw.beans.FeedListenerBean">
</bean>
<bean id="TriggerBean" class="com.fixgw.test.TriggerBean">
</bean>
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean" >
<property name="locateExistingServerIfPossible" value="true" />
</bean>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=TriggerBean1" value-ref="TriggerBean" />
</map>
</property>
<property name="server" ref="mbeanServer" />
</bean>
</beans>
And a Trigger bean which I want it's public method to be exposed in the JMX:
public class TriggerBean implements IJmxTriggerBean
{
static Logger logger = Logger.getLogger(TriggerBean.class);
FeedListenerBean fe = null;
public void start()
{
try
{
// init();
PropertyConfigurator.configure(FixGWConstants.LOG4J_PATH);
ApplicationContext context = new ClassPathXmlApplicationContext(FixGWConstants.APPLICATION_CONTEXT_XML);
fe = (FeedListenerBean) context.getBean("FeedListenerBean");
Thread t=new Thread()
{
public void run()
{
while (true)
{
System.out.println("a");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.run();
doTheListen();
}
catch (Throwable t)
{
logger.error(t);
System.out.println(t);
}
}
public void doTheListen()
{
fe.listen();
}
}
package com.finbird.fixgw.test;
public interface IJmxTriggerBean
{
public void doTheListen();
}
Is that enough for configuration?
Now to which local address:port should I connect in order to access the console?
thanks,
ray
You have the JMX server, but you need an HTMLAdapter to view these beans via a browser. e.g. from this article:
<bean id="htmlAdaptor" class="com.sun.jdmk.comm.HtmlAdaptorServer" init-method="start" />
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="adaptor:name=htmlAdaptor" value-ref="htmlAdaptor" />
<entry key="bean:name=calculatorConfigBean" value-ref="calculatorConfig" />
</map>
</property>
<property name="server" ref="mbeanServer" />
</bean>
Note that I'm assuming HTML/browser access here. See here for an Oracle tutorial with an adapter configured in code.

Categories

Resources