📄 messagesender.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();
//message.setBooleanProperty ("NewApplicant", newApplicant);
String xml = applicantXML (applicant, name, email, summary, location, skills, newApplicant);
message.setText(xml);
queueSender.send(message);
}
public void sendJob(String ref, String customer, boolean newJob) throws JMSException {
TextMessage message = queueSession.createTextMessage();
message.setBooleanProperty ("NewJob", newJob);
message.setText(ref+"+"+customer);
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 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 + -