Java WebService Implementation read/write Redis database

Source: Internet
Author: User
Tags redis server wsdl

First, the preparatory work

1, MyEclipse10

2. JDK 1.7.0

3, apache-tomcat-6.0.13

Second, create the service side

1. Create "Web Service Project", named "Theservice".


2. Create the "class" class, named "Servicehello", located under the "Com.wty.service" package.


3, write the method for the client to call, that is, compile the method code.

Package Com.wty.service;import javax.jws.webservice;//Pack do not quote the wrong import Javax.xml.ws.endpoint;import Redis.clients.jedis.jedis;import redis.clients.jedis.Transaction; @WebService//Annotations Don't forget to add the public class Servicehello {/* * * These methods are called by the client method, the method name arbitrary command can be */public string GetValue (string name) {return "I call:" +name;} Private Jedis jedis;//Non-slicing client connection public void Startredis () {Jedis = new Jedis ("127.0.0.1", 6379);} /** * String Read and write operation * * @param key * @return */public string Get (String key) {string getstr = Jedis.get (key); return getstr;} public string Set (string key, String value) {String setstr = Jedis.set (key, value); return setstr;}  /** * Hash Read and write operation * * @param hash * @param key * @return */public string Hash_get (String key, String field) {String getstr = Jedis.hget (key, field); return getstr;} Public Long Hash_set (string key, String field, String value) {Long setstr = Jedis.hset (key, field, value); return setstr;} /** * Hincrby Value-added operation * * @param key * @param field * @param value * @return */public Long hincre (String key, String field, int value) {Long incrstr = Jedis.hincrby (key, field, value); return incrstr;} /** * Multi Transaction operation * * @return */public Transaction multi () {Transaction Tranopera = Jedis.multi (); return tranopera;} /** * Clear 0 Operation * @return */public string flushdb () {String flushopera = Jedis.flushdb (); return flushopera;} /** * Select database * @param index * @return */public string selectdb (int index) {string selectopera = Jedis.select (index); return Selectopera;} public static void Main (string[] args) {//The following sentence is important for publishing a server, it is recommended to use port 8080 because Tomcat defaults to 8080,endpoint.publish ("HTTP// 219.245.72.20:8080/service/servicehello ", New Servicehello ()); SYSTEM.OUT.PRINTLN ("service success!");}}


4. Tomcat Deployment

If you are only testing natively, you do not have to deploy to Tomcat, just compile the service-side program. Deployed to Tomcat for non-native access to WebService.


5. Suspend Tomcat and test for the successful launch of Tomcat



6. Compile the service-side project

Description: If compilation failed, set the JDK referenced by the project to a higher version

Compilation succeeded.


7. Test results

Test Address:http://219.245.72.20:8080/Service/ServiceHello?wsdl


third, generate the client

The client is responsible for invoking the newly created WebService, who wants to call and build on who's machine.

1. create "Web Service Project", named "theclient".

At this point the Theclient project is empty.


2. The command Prompt window executes the build command.

format: wsimport-s "src directory" - P "Generate package name for class" -keep "WSDL Publishing Address"

Example:

Wsimport-s f:\\workspaces\\theclient\\src-p Com.wty.client-keep http://219.245.72.20:8080/Service/ServiceHello?wsdl

Description

1) "src directory" address cannot contain spaces

2) " WSDL Publishing Address" Don't miss it. ? wsdl "

Resolved as follows:



3, refresh the Theclient project, check the auto-generated class (where the Java file with the response suffix is the service-side method)


Iv. final Test

1. Create the "class" class, named "Servicetest", located under the "Com.wty.test" package.


2, write the test method, call the service-side method, and print the returned results.

Package Com.wty.test;import Com.wty.client.servicehello;import Com.wty.client.servicehelloservice;public class servicetest {/** * @param args */public static void main (string[] args) {Servicehello Hello = new Servicehelloservice (). GE Tservicehelloport (); Hello.startredis (); String result = Hello.get ("1011"); SYSTEM.OUT.PRINTLN (result);}}

Because of the method of invoking the server's read-write Redis servers, such as the Get ("1011") method,

Remember to open the Redis server here.


As long as in a local area network, other machines can also access any method of calling the above webservice.

v. Reference Reference

http://www.iteye.com/topic/1135747

Java WebService Implementation read/write Redis database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.