The JAXM API conforms to the Simple Object Access Protocol (SOAP) 1.1 specification and the SOAP with Attachments specification. The complete JAXM API is presented in two packages:
javax.xml.soap
– the package defined in the SOAP with Attachments API for Java (SAAJ) 1.1 specification. This is the basic package for SOAP messaging, which contains the API for creating and populating a SOAP message.javax.xml.messaging
– the package defined in the JAXM 1.1 specification. This package contains the API needed for using a messaging provider and thus for being able to send one-way messages.JAX-RPC stands for Java API for XML-based RPC. It's an API for building Web services and clients that used remote procedure calls (RPC) and XML.
QueueConnectionFactory myQConnFactory = new com.sun.messaging.QueueConnectionFactory();
or locate in JNDI:
Context ctx = new InitialContext(); QueueConnectionFactory myQConnFactory = (QueueConnectionFactory) ctx.lookup("MyQueueConnectionFactory");
QueueConnection myQConn = myQConnFactory.createQueueConnection();
QueueSession myQSess = myQConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender myQueueSender = myQSess.createSender(myQueue);
TextMessage myTextMsg = myQSess.createTextMessage(); myTextMsg.setText("Hello World"); myQueueSender.send(myTextMsg);
QueueReceiver myQueueReceiver = myQSess.createReceiver(myQueue);
and start it
myQConn.start();
Message msg = myQueueReceiver.receive();
TextMessage txtMsg = (TextMessage) msg; String text = txtMsg.getText();
myQSess.close(); myQConn.close();
A connection:
For this reason, it's a good idea to use as few connections as possible
A session:
The requirement that sessions be operated on by a single thread at a time places some restrictions on the combination of producers and consumers that can use the same session. In particular, if a session has an asynchronous consumer, it may not have any other synchronous consumers. The Java Messaging Specification mandates that a session not be operated on by more than one thread at a time. This leads to the following restrictions:
onMessage()
method (the message listener). The only call that you can make outside the message listener is to close the session.
AUTO_ACKNOWLEDGE
The system automatically acknowledges a message once the consumer has processed it. This mode guarantees at most one redelivered message after a provider failure.CLIENT_ACKNOWLEDGE
The application controls the point at which messages are acknowledged. All messages processed in that session since the previous acknowledgement are acknowledged. If the message server fails while processing a set of acknowledgments, one or more messages in that group might be redelivered. Using this mode is similar to using transactions, except there is no guarantee that all acknowledgments will be processed together if a provider fails during processing.DUPS_OK_ACKNOWLEDGE
This mode instructs the system to acknowledge messages in a lazy manner. Multiple messages can be redelivered after a provider failure.NO_ACKNOWLEDGE
In this mode, the broker considers a message acknowledged as soon as it has been written to the client. The broker does not wait for an acknowledgement from the receiving client. This mode is best used by typic subscribers who are not worried about reliability.
AddressListBehavior=RANDOM
SOAP is a protocol that allows the exchange of structured data between peers in a decentralized, distributed environment.
JAXMServlet
to simplify SOAP service endpoint developmentMessageTransformer.SOAPMessageIntoJMSMessage(SOAPMessage, Session)
and MessageTransformer.SOAPMessageFromJMSMessage(Message, MessageFactory)
to perform SOAP-to-JMS transformations.See Creating a SOAP Web Service using Java6 for practical information.
soapMessage.addAttachmentPart(soapMessage.createAttachmentPart(new DataHandler(new URL("http://greatproducts.com/pics/img.jpg"))));
soapBody.addFault().setFaultString("Message does not have necessary info");
SOAPConnectionFactory
object.SOAPConnection
object.MessageFactory
object and use it to create a message.SOAPMessage response = soapConnection.call(soapMessage, new URL("http://bat.server.com/service"));
or
SOAPMessage response = soapConnection.call(soapMessage, new javax.xml.messaging.URLEndpoint("http://somehost/myServlet"));
ProviderConnectionFactory
object.ProviderConnection
object from the provider connection factory.MessageFactory
object from the provider connection and use it to create a message.providerConnection.send(soapMessage);
SOAPMessage
object will come with certain headers already set. Also a profile implementation may provide API that makes it easier to create a header and set its content. The JAXM reference implementation includes APIs for both the ebXML and SOAP-RP profiles: String[] supportedProfiles = providerConnection.getMetaData().getSupportedProfiles(); if (supportedProfiles.length > 0 && "ebxml".equals(supportedProfiles[0])) { MessageFactory factory = pcCon.createMessageFactory(supportedProfiles[0]); com.sun.xml.messaging.ebxml.EbXMLMessageImpl /* is a part of JWSDP */ message = (com.sun.xml.messaging.ebxml.EbXMLMessageImpl) factory.createMessage(); }
SOAPHeader
object.
JAX-RPC supports all primitives except char
and java.lang.Char
, all collections and user types if user type confirm the following restrictions:
java.rmi.Remote
interface.The class may contain public, private, or protected fields. For its value to be passed (or returned) during a remote call, a field must meet these requirements:
A service definition interface (remote interface) declares the methods that a remote client may invoke on the service. The interface must conform to a few rules:
java.rmi.Remote
interface.public final static
.java.rmi.RemoteException
or one of its subclasses. The methods may also additionaly throw service-specific exceptions.
wscompile
before runtime. javax.xml.rpc.Stub stub = (javax.xml.rpc.Stub) (new MyHello_Impl().getHelloIFPort()); HelloService hello = (HelloService) stub; System.out.println(hello.sayHello("Duke!"));
The code in this example is implementation-specific and might not be portable.
final String NS = "http://proxy.org/wsdl"; final String SERVICE_NAME = "HelloWorld"; final String PORT_NAME = "HelloPort"; final URL HELLO_WSDL_URL = new URL("http://localhost:8080/ProxyHelloWorld.wsdl"); javax.xml.rpc.Service helloService = javax.xml.rpc.ServiceFactory.newInstance().createService(HELLO_WSDL_URL, new QName(NS, SERVICE_NAME)); HelloService myProxy = (HelloService) helloService.getPort(new javax.xml.namespace.QName(NS, PORT_NAME), HelloService.class); System.out.println(myProxy.sayHello("Duke!"));
final String ENDPOINT = "http://localhost:8080/dynamic-jaxrpc/dynamic"; final String SERVICE_NAME = "Hello"; final String PORT_NAME = "HelloPort"; final String BODY_NS = "http://dynamic.org/wsdl"; final String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri"; final String XSD_NS = "http://www.w3.org/2001/XMLSchema"; final String ENCODING_NS = "http://schemas.xmlsoap.org/soap/encoding/"; javax.xml.rpc.ServiceFactory factory = javax.xml.rpc.ServiceFactory.newInstance(); javax.xml.rpc.Service service = factory.createService(new QName(SERVICE_NAME)); javax.xml.rpc.Call call = service.createCall(new QName(PORT_NAME)); call.setTargetEndpointAddress(ENDPOINT); call.setProperty(javax.xml.rpc.Call.SOAPACTION_USE_PROPERTY, new Boolean(true)); call.setProperty(javax.xml.rpc.Call.SOAPACTION_URI_PROPERTY, ""); call.setProperty(ENCODING_STYLE_PROPERTY, ENCODING_NS); QName QNAME_TYPE_STRING = new QName(XSD_NS, "string"); call.setReturnType(QNAME_TYPE_STRING); call.setOperationName(new QName(BODY_NAMESPACE_VALUE, "sayHello")); call.addParameter("String_1", QNAME_TYPE_STRING, javax.xml.rpc.ParameterMode.IN); String[] params = { "Duke!" }; System.out.println((String) call.invoke(params));
"Девица не хочет лезть в Окно" – device not compatible with Windows.