⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagesender.java

📁 21天精通Java,这是一本英文书
💻 JAVA
字号:
package agency;

import javax.naming.*;
import javax.jms.*;
import java.util.*;
import data.*;

public class MessageSender {

    private QueueConnection queueConnection;
    private QueueSession queueSession;
    private QueueSender queueSender;
    private Queue queue;

    public MessageSender(String jndiFactory, String jndiQueue) throws JMSException, NamingException {
        Context context = new InitialContext();
        QueueConnectionFactory queueFactory = (QueueConnectionFactory)context.lookup(jndiFactory);
        queueConnection = queueFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queue = (Queue)context.lookup(jndiQueue);
        queueSender = queueSession.createSender(queue);
    }

    public void sendApplicant(String applicant, String name, String email,
                String summary, String location, Collection skills, boolean newApplicant) throws JMSException {
        TextMessage message = queueSession.createTextMessage();
        String xml = applicantXML (applicant, name, email, summary, location, skills, newApplicant);
        message.setText(xml);
        queueSender.send(message);
    }

    public void sendJob(String ref, String customer, String location,
           String description, Collection skills, boolean newJob) throws JMSException {
System.out.println ("ref:" + ref);
System.out.println ("customer:" + customer);
System.out.println ("location:" + location);
System.out.println ("description:" + description);

        TextMessage message = queueSession.createTextMessage();
        String xml = jobXML (customer, ref, location, description, skills, newJob);
        message.setText(xml);
        queueSender.send(message);
    }

    public void close() throws JMSException {
         //Send a non-text control message indicating end of messages
        queueSender.send(queueSession.createMessage());

        queueSender.close();
        queueSession.close();
        queueConnection.close();
    }

    private final String XMLVersion = "<?xml version ='1.0'?>";

    private String applicantXML (String applicant, String name, String email,
                String summary, String location, Collection skills, boolean newApplicant) throws JMSException {
        StringBuffer xmlText = new StringBuffer(XMLVersion);
        addTag(xmlText, "applicantSummary", "");
        String newApplicantString = newApplicant?"true'":"false'";
        String attributes = " login='" + applicant + "' new='" + newApplicantString;
        addTag(xmlText, "applicant", attributes);
        addElement(xmlText, "name", name);
        addElement(xmlText, "email", email);
        addElement(xmlText, "summary", summary);
        addElement(xmlText, "location", location);
        Iterator it = skills.iterator();
        while (it.hasNext()){
            SkillLocal skill = (SkillLocal) it.next();
            addElement(xmlText, "skill", skill.getName());
        }
        addEndTag(xmlText, "applicant");
        addEndTag (xmlText, "applicantSummary");
        return xmlText.toString();
    }

    private String jobXML (String customer, String ref, String location,
               String description, Collection skills, boolean newJob) throws JMSException {
        StringBuffer xmlText = new StringBuffer(XMLVersion);
        addTag(xmlText, "jobSummary", "");
        String newJobString = newJob?"true'":"false'";
        String attributes = " customer='" + customer + "' reference ='" + ref + "' new='" + newJobString;
        addTag(xmlText, "job", attributes);
        addElement(xmlText, "location", location);
        addElement(xmlText, "description", location);
        Iterator it = skills.iterator();
        while (it.hasNext()){
            SkillLocal skill = (SkillLocal) it.next();
            addElement(xmlText, "skill", skill.getName());
        }
        addEndTag(xmlText, "job");
        addEndTag (xmlText, "jobSummary");
        return xmlText.toString();
    }


    private void addTag (StringBuffer buf, String tag, String attributes) {
        buf.append("<");
        buf.append(tag);
        buf.append(attributes);
        buf.append(">");
    }
    private void addEndTag (StringBuffer buf, String tag) {
        buf.append("</");
        buf.append(tag);
        buf.append(">");
    }
    private void addElement (StringBuffer buf, String tag, String contents) {
        addTag(buf, tag, "");
        buf.append (contents);
        addEndTag (buf, tag);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -