Spring calls RMI
RMI (Remote Method Invocation) Remote method call to realize remote communication between JAVA applications. The following describes how to use RMI using Spring.
The structure of the package is as follows:
Define the calling interface
public interface UserDao { public String getUser(String username)throws Exception;}Interface implementation class
public class UserDaoImplimples UserDao { public String getUser(Stringusername)throws Exception { return"test:"+username; }}(1) Configure RMI service:
applicationContext-rmi-server.xml <beanid="userDaoImpl"class="com.rmi.UserDaoImpl"/> <beanid="userDaoImpl_Exporter"class="org.springframework.remoting.rmi.RmiServiceExporter"> <propertyname="service"ref="userDaoImpl"/> <propertyname="serviceName"value="rmi/userDaoImpl"/> <propertyname="serviceInterface"value="com.rmi.UserDao"/> <propertyname="registryPort"value="8819"/> </bean>
(2) Start RMI service:
public class RmiServer { publicstaticfinal ApplicationContextcontext =new ClassPathXmlApplicationContext("applicationContext-rmi-server.xml"); publicstaticvoid main(String[] args) { }}(3) Access RMI services
applicationContext-rmi-client.xml <beanid="userDaoImpl_client"class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <propertyname="serviceUrl"value="rmi://localhost:8819/rmi/userDaoImpl"/> <propertyname="serviceInterface"value="com.rmi.UserDao"/> <propertyname="refreshStubOnConnectFailure"value="true"/> <propertyname="lookupStubOnStartup"value="false"/> </bean> public class RmiClient { publicstaticvoid main(String[] args) { ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext-rmi-client.xml"); UserDao userDao = (UserDao)context.getBean("userDaoImpl_client"); if(userDao !=null){ try { System.out.println(userDao.getUser("li")); } catch (Exception e) { e.printStackTrace(); } } }} spring call quartz
1.quartz is a job scheduling framework, quartz integrated with spring, which is very convenient to use.
2. Write execution classes
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class QuartzTest { public static void main(String[] args) { System.out.println("Test start."); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-quartz.xml"); //If the lazy-init of the startQuetz bean is set to false in the configuration file Then there is no need to instantiate //context.getBean("startQuertz"); System.out.print("Test end.."); } }3. Write configuration files
<!-- Working class to call --> <bean id="quartzJob"/> <!-- Define the method to call objects and call objects --> <bean id="jobDetail"> <property name="targetObject" ref="quartzJob" /> <property name="targetMethod" value="work" /> </bean> <!-- Define the trigger time --> <bean id="doTime"> <property name="jobDetail"> <ref bean="jobDetail"/> </property> <!-- cron expression--> <property name="cronExpression"> <value>2/5 44-46 22,23 9 9 ? 2012</value> <!-- From left to right are: seconds, minutes, time, day, month, year, and week? The numbers can only be used in the day and week domains, but cannot be used simultaneously in these two domains. Can you think? The character is "I don't care what value is on the field." This is different from an asterisk, which indicates every value on the field. ? means that no value is specified for this field. A comma (,) is used to specify a list of values to a field. For example, using values 0,15,30,45 in the second field means that a trigger is triggered every 15 seconds. Slashes (/) are used for incremental timetables. We just used commas to represent increments every 15 minutes, but we can also write them as 0/15. Scribble (-) is used to specify a range. For example, 3-8 on the hour domain means "3, 4, 5, 6, 7 and 8 points." The values of the domain do not allow rewinding, so values like 50-10 are not allowed. The asterisk (*) indicates that you want to include all legal values on this field. For example, using an asterisk on the month domain means that this trigger will be triggered every month. The letter L indicates the last value allowed on a field. It is only supported by the daily and weekly domains. The W character represents weekdays (Mon-Fri) and can only be used in the daily domain. It is used to specify the weekday closest to the specified day (non-Saturday). # characters can only be used in the peripheral domain. It is used to specify which day of the week in a specified month. For example, if you specify the value of the weekly field to be 6#3, it means the third Friday of a certain month (6=Friday, #3 means the third week of the month). --> </property> </bean> <!-- General management class If lazy-init='false' is used, the container will execute the scheduler --> <bean id="startQuertz" lazy-init="false" autowire="no"> <property name="triggers"> <list> <ref bean="doTime"/> </list> </property> </bean>
4. About cronExpression expressions
<!-- cron expression--> <property name="cronExpression"> <value>2/5 44-46 22,23 9 9 ? 2012</value> <!-- From left to right are: seconds, minutes, time, day, month, year, and week? The numbers can only be used in the day and week domains, but cannot be used simultaneously in these two domains. Can you think? The character is "I don't care what value is on the field." This is different from an asterisk, which indicates every value on the field. ? means that no value is specified for this field. A comma (,) is used to specify a list of values to a field. For example, using values 0,15,30,45 in the second field means that a trigger is triggered every 15 seconds. Slashes (/) are used for incremental timetables. We just used commas to represent increments every 15 minutes, but we can also write them as 0/15. Scribble (-) is used to specify a range. For example, 3-8 on the hour domain means "3, 4, 5, 6, 7 and 8 points." The values of the domain do not allow rewinding, so values like 50-10 are not allowed. The asterisk (*) indicates that you want to include all legal values on this field. For example, using an asterisk on the month domain means that this trigger will be triggered every month. The letter L indicates the last value allowed on a field. It is only supported by the daily and weekly domains. The W character represents weekdays (Mon-Fri) and can only be used in the daily domain. It is used to specify the weekday closest to the specified day (non-Saturday). # characters can only be used in the peripheral domain. It is used to specify which day of the week in a specified month. For example, if you specify the value of the weekly field to be 6#3, it means the third Friday of a certain month (6=Friday, #3 means the third week of the month). -->