Kauj ruam mus integrate rau lub caij nplooj hlav & Java lus Service

Spring and JMS

Lub caij nplooj ntoos hlav Series – Kawm JMS yog piv txwv

Txheej txheem cej luam

Nyob rau hauv tsab xov xwm no peb yuav npog JMS integration nrog caij nplooj ntoos hlav. Peb yuav mloog zoo dua rau tus siv Tshooj coding. Qhov no yog pab cov developers sim hands-on coding thiab kev JMS nyob rau hauv lawv qhov project.

Java messaging yog siv rau messaging lub hom phiaj rau feem ntau java thiab J2E daim ntawv thov. Lub caij nplooj ntoos hlav yuav muab ib qho abstraction rau lub JMS txheej txheem uas yuav ua rau nws yooj yim mus saib tau cov lus queue los yog lub npe (pheej hu ua destination) thiab luam tawm lus rau lo lus uas peb.







Kauj ruam ua raws li

JmsTemplate yog cov hoob kawm siv rau messaging purport hauv caij nplooj ntoos hlav. Cov cuab yeej jmsTemplate yuav raug wired nrog ib lo lus org.springframework.jms.tub ntxhais JmsTemplate siv txhaj koob tshuaj txhaj

Cov sendSettlementS (sendSettment)() Txoj kev siv hauv lub JmsTemplate xa() txoj kev xa cov lus. Txoj kev no yuav siv sij hawm li ntawm org.springframework.jms.tub ntxhais Messagecreator

Tom qab code snippet qhia tau hais tias tus wiring ntawm qhov configuration

<taum daim id =”kev them nyiaj”

hoob =”com.springinaction.training.service.PaymentServiceImpl”>

<lub npe ntawm tus kheej =”jmSTemplate”>

<nyob taum =”jmSTemplate”/>

</khoom>

<taum>

Tom qab no yog cov kauj ruam mus cuag lub caij nplooj ntoos hlav thiab JMS siv ib daim ntawv thov kev pab.

  • Configure cov nram qab no JMS kev pab hauv daim ntawv thov kev pab.

Sib txuas- Jms/conctionFactory thiab Queue Destination- jms/testue

  • Ua daim ntawv thov dynamic daim ntawv thov kev pab
  • Lub caij nplooj ntoos hlav.jar, Hom-logging.jar thiab log4j-1.15.jar
  • Tsim lub InvoiceQueUeNder hoob kawm nyob rau hauv lub pob ris tsho. Cov chav kawm ntawv InvoiceQueunder siv los xa lus. Tus JMSTemplate txhaj mus rau hauv invoiceQueunder siv IOC tham.

pob techalpine.jms;

Import javax.jmSException.JMSException;

Import javax.jms.jms.Hais;

import javax.jms.qhib;

Import.springframework.jms.JmsTemplate;

import.springframework.jms.tub ntxhais MessageCreator;

Pej xeem hoob kawm InvoiceQueunder {

ntiav JmsTemplate jmsTemplate;

Pej xeem tsis txhob setJmsemplate(JmsTemplate jmsTemplate) {

Qhov no.jmsTemplate = jmsTemplate;

}

pej xeem yuav tsum tsis txhob xa cov sendMesage() {

Lus qhia messagecreator=tshiab Messagecreator() {

pej xeem lus tsim(Zaug sib kho kom haum) throws

JMSException {

rov qab sib kho..ua ib qho(“Kuv xa Invoice lus”);}

};

jmSTemplate.xa(“jms/testue”, lus qhia);

}

}







  • Tsim lub InvoiceMDB hoob kawm nyob rau hauv lub pob ris tsho.

pob techalpine.jms;

Import javax.jmSException.JMSException;

Import javax.jms.jms.Hais;

import javax.jms.MessageListener;

Import javax.jms.TextMessagesage;

Pej xeem hoob kawm InvoiceMDB dluab Messagestener {

Pej xeem void onmessage(lus hais) {

sim {

System.out.println(((TextMessagesage) xov).tau txais());

System.out.println(“Nyob zoo JMS”);

} ntes (JMSException ex) {

pov RuntimeException tshiab(ex);

}

}

}

  • Cov lus hauv qab no yuav configured hauv lub caij nplooj ntoos hlav configuration muaj npe ua ntawv thov.xml.

&ltbean id=”jndiTemplate” hoob =”org.springframework.jndi.JndiTemplate”>

&ltproperty npe=”ib puag ntiag”>

&lwj>

&lptrop key=”java.naming.factory.thawj zaug”&gtweblogic.jndi.WLInitialContexTFactory</prop>

&lptrop key=”java.naming.provider.url”&gtt3://localhost:7001</prop>

</vam meej>

</khoom>

</taum>

  • JndiOjbectFactoryBean siv los saib lub JNDI khoom rau startup thiab cache nws. Interface siv los configure sib txuas lus

&ltbean id=”queueConnectionory” hoob =”org.springframework.jndi.JndiOjbectFactoryBean”>

&ltproperty npe=”jndiTemplate”>

&taum ltref bean=”jndiTemplate” />

</khoom>

&ltproperty npe=”jindName”>

&ltvalue&gtms/conctionFactory</tus nqi>

</khoom>

</taum>

  • Destinationsolver siv los ntawm JmsTemplate los daws cov npe

&ltbean id=”jmsDestinationResolver” hoob =”org.springframework.jms.txhawb.JndiDestinationResvolver”>

&ltproperty npe=”jndiTemplate”>

&taum ltref bean=”jndiTemplate” />

</khoom>

&ltproperty npe=”cache”>

&ltvalue&tsis ntseeg</tus nqi>

</khoom>

</taum>

  • Tam sim no lub JMSTemplate yog siv los xa lus. Tom qab no yog ib tug code snippet

&ltbean id=”invoiceQueueTemplate” hoob =”org.springframework.jms.tub ntxhais JmsTemplate”>

&ltproperty npe=”kev sib txuas”>

&taum ltref bean=”queueConnectionory” />

</khoom>

&ltproperty npe=”destinations”>

&taum ltref bean=”jmsDestinationResolver” />

</khoom>

</taum>

  • Tam sim no tus Queue configuration yog muab hauv qab no

&ltbean id=”invoiceQueue” hoob =”org.springframework.jndi.JndiOjbectFactoryBean”>

&ltproperty npe=”jndiTemplate”>

&taum ltref bean=”jndiTemplate” />

</khoom>

&ltproperty npe=”jindName”>

&ltvalue&gtms/testQueue</tus nqi>

</khoom>

</taum>

  • Tam sim no tom qab configuring tag nrho cov xeem uas tsim nyog cov ntsiab lus kawg yuav ua raws li cov lus qhia

<?xml version =”1.0″ encoding =”UTF-8″?>

<!DOCTYPE taum PUBLIC “-//CAIJ NPLOOJ NTOOS HLAV / / DTD BEAN / / EN”

“http://www.springframework.org/dtd/Spring-beans.dtd”>

&ltbeans>

&ltbean id=”hauv plawv nroog” hoob =”ris tsho. InvoiceMB” />

&ltbean id=”jndiTemplate” hoob =”org.springframework.jndi.JndiTemplate”>

&ltproperty npe=”ib puag ntiag”>

&lwj>

&lptrop key=”java.naming.factory.thawj zaug”&gtweblogic.jndi.WLInitialContexTFactory</prop>

&lptrop key=”java.naming.provider.url”&gtt3://localhost:7001</prop>

</vam meej>

</khoom>

</taum>

&ltbean id=”queueConnectionory” hoob =”org.springframework.jndi.JndiOjbectFactoryBean”>

&ltproperty npe=”jndiTemplate”>

&taum ltref bean=”jndiTemplate” />

</khoom>

&ltproperty npe=”jindName”>

&ltvalue&gtms/conctionFactory</tus nqi>

</khoom>

</taum>

&ltbean id=”jmsDestinationResolver” hoob =”org.springframework.jms.txhawb.JndiDestinationResvolver”>

&ltproperty npe=”jndiTemplate”>

&taum ltref bean=”jndiTemplate” />

</khoom>

&ltproperty npe=”cache”>

&ltvalue&tsis ntseeg</tus nqi>

</khoom>

</taum>

&ltbean id=”invoiceQueueTemplate” hoob =”org.springframework.jms.tub ntxhais JmsTemplate”>

&ltproperty npe=”kev sib txuas”>

&taum ltref bean=”queueConnectionory” />

</khoom>

&ltproperty npe=”destinations”>

&taum ltref bean=”jmsDestinationResolver” />

</khoom>

</taum>

&ltbean id=”jmInvoiceSeNder” hoob =”ris tsho. InvoiceQueunder”>

&ltproperty npe=”jmSTemplate”>

&taum ltref bean=”invoiceQueueTemplate” />

</khoom>

</taum>

&ltbean id=”invoiceQueue” hoob =”org.springframework.jndi.JndiOjbectFactoryBean”>

&ltproperty npe=”jndiTemplate”>

&taum ltref bean=”jndiTemplate” />

</khoom>

&ltproperty npe=”jindName”>

&ltvalue&gtms/testQueue</tus nqi>

</khoom>

</taum>

&ltbean id=”Invoicelistener (Invoicelistener)” hoob =”org.springframework.jms.mloog.defaultMesListenCtainer”>

&ltproperty npe=”cov neeg ua hauj lwm” tus nqi =”5″ />

&ltproperty npe=”kev sib txuas” nyob =”queueConnectionory” />

&ltproperty npe=”kev ntxub ntxaug” nyob =”invoiceQueue” />

&ltproperty npe=”lus Askiv” nyob =”hauv plawv nroog” />

</taum>

</taum>

  • Lub caij nplooj zeeg tas los no yog cov lus tseem fwv xa lus.

pob techalpine.jms;

ntshuam java.io.IOException;

Chaw nres nkoj javax.servlet.ServletException;

import javax.servlet.http.http.HtpServertRequest;

import javax.servlet.http.http.HtpServertResponse;

import.springframework.web.context.Webpaplication;

Import.springframework.web.web.context.support.Webbappontils;

Pej xeem hoob kawm InvoiceSeNderServlet extends javax.servlet.http.http.HtpServlet

DVR javax.servlet.servlet.servlet {

kev tiv thaiv(Thov HttpServletRequest,

HttpServletResponse teb) pov tseg ServletException, IOException {

Webapaplication ctx = Webapponteclication

.Tau kis tau tus kab mob CWebAplication(qhov no... getServletContex());

InvoiceQueueSender xa = (InvoiceQueunder) ntxhuav

.taum pauv(“jmInvoiceSeNder”);

xa...();

}

}

Tam sim no configure lub web site.xml li nram qab no thiab khiav lub servlet

<?xml version =”1.0″ encoding =”UTF-8″?>

&ltweb-app id=”WebApp_ID” version=”2.4″ xmlns =”http://java.sun.com/xml/ns/j2ee” xmlns:xsi =”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation =”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>

&ltdisplay-npe>

caij nplooj ntoos hlav</zaub>

&hlwb>

&ltdescription>

</hauj lwm lawm>

&ltdisplay-npe>

InvoiceSeNderSvlet</zaub>

&npe&gtInvoiceSeSeNdervlet</tis npe>

&ltsvervlet-class>

ris tsho. InvoiceSeNderSvlet</chav kawm ntawv>

</pw tsaug zog>

&ltslevert-mapping>

&npe&gtInvoiceSeSeNdervlet</tis npe>

&tus qauv lturl-qauv>/InvoiceSeNderSvlet</tus qauv>

</qhwv-mapping>

&ltwelcome-file-list>

&ltwelcome-file&gintdex.html</txais tos-ntaub ntawv>

</txais tos-teev>

&ltlistener>

&chav kawm ntawv ltlistener-class&gtorg.springframework.web.context.ContextLoaderListener</mloog kawm>

</mloog>

</Web-app>

Tus console yuav tso saib cov lus nram qab no

Nyob zoo JMS







Xaus

Tsab xov xwm no piav li cas JMS yuav tau integrated yooj yim nrog lub caij nplooj ntoos hlav. JMS yog ib qhov tseem ceeb tshaj plaws feature siv nyob rau ntau daim ntaub ntawv. Li ntawd, nws zoo kawm JMS, thiab seb nws yuav siv tau cov caij nplooj ntoos hlav. Raws li, Lub caij nplooj ntoos hlav yog lub ntiaj teb nyiam heev tshaj plaws nyob hauv lub ntiaj teb Java, Nws tseem ceeb heev uas yuav tau nkag siab txog qhov txhawb lub ntsiab lus.

============================================= ============================================== Yuav zoo TechAlpine phau ntawv rau Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Txaus siab rau qhov blog? Tshaj tawm lus thov :)

Follow by Email
LinkedIn
LinkedIn
Share