public bool DeleteMessage(string queueName, string messageHandle) { bool result = false; try { String accessKeyId = ConfigurationSettings.AppSettings["AmazonAccessKeyID"]; String secretAccessKey = ConfigurationSettings.AppSettings["AmazonSecretAccessKey"]; AmazonSQS service = new AmazonSQSClient(accessKeyId, secretAccessKey); // Create delete message request object DeleteMessageRequest request = new DeleteMessageRequest(); // Set queue name containing item to be deleted request.QueueName = queueName; // Set handle of message to be deleted request.ReceiptHandle = messageHandle; // Delete message DeleteMessageResponse response = service.DeleteMessage(request); Debug.WriteLine("Service Response"); Debug.WriteLine("\tDelete Message Response"); // If message response if (response.IsSetResponseMetadata()) { Debug.WriteLine("\tResponse Metadata"); ResponseMetadata responseMetadata = response.ResponseMetadata; if (responseMetadata.IsSetRequestId()) { Debug.WriteLine(String.Format("\tRequest Id: {0}", responseMetadata.RequestId)); } } } catch (AmazonSQSException ex) { Debug.WriteLine("Caught Exception: " + ex.Message); Debug.WriteLine("Response Status Code: " + ex.StatusCode); Debug.WriteLine("Error Code: " + ex.ErrorCode); Debug.WriteLine("Error Type: " + ex.ErrorType); Debug.WriteLine("Request ID: " + ex.RequestId); Debug.WriteLine("XML: " + ex.XML); } return result; } |
类
|
描述
|
AddresssEntity
|
包含一个客户或业务地址
|
CustomerOrderEntity
|
包含客户订单(购买订单)
|
CustomerOrderDetailEntity
|
包含每个客户订单(购买订单)的商品细节
|
MessageQueueEntity
|
包含消息队列项(即购买订单或订单汇总)
|
OrderSummaryEntity
|
包含订单发货汇总
|
OrderSummaryDetailEntity
|
包含订单发货汇总的商品细节
|
protected ArrayList<HashMap<String, String>> getMessageQueueItems(String queueName) { ArrayList<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>(); try { String accessKeyId = AppConfig.getProperty("AmazonAccessKeyID"); String secretAccessKey = AppConfig.getProperty("AmazonSecretAccessKey"); AmazonSQS service = new AmazonSQSClient(accessKeyId, secretAccessKey); // get messages from queue ReceiveMessageRequest request = new ReceiveMessageRequest(); request.setQueueName(queueName); request.setVisibilityTimeout(1); ReceiveMessageResponse response = service.receiveMessage(request); // if received response if (response.isSetReceiveMessageResult()) { System.out.println("\tReceive Message Result"); ReceiveMessageResult receiveMessageResult = response.getReceiveMessageResult(); // get all messages and iterate through them List<Message> messageList = receiveMessageResult.getMessage(); for (Message message : messageList) { // build hashmap containing each message HashMap<String, String> row = new HashMap<String, String>(); if (message.isSetMessageId()) { row.put(COLUMN_ID, message.getMessageId()); System.out.println("\t\tMessageId: " + message.getMessageId()); } if (message.isSetReceiptHandle()) { row.put(COLUMN_HANDLE, message.getReceiptHandle()); } if (message.isSetBody()) { row.put(COLUMN_MESSAGE, message.getBody()); } // add row results.add(row); } } } catch (AmazonSQSException ex) { System.out.println("Caught Exception: " + ex.getMessage()); System.out.println("Response Status Code: " + ex.getStatusCode()); System.out.println("Error Code: " + ex.getErrorCode()); System.out.println("Error Type: " + ex.getErrorType()); System.out.println("Request ID: " + ex.getRequestId()); System.out.println("XML: " + ex.getXML()); } return results; } |
|
public class PurchaseOrderSerializer { public CustomerOrderEntity deserializePO(MessageQueueEntity message) { CustomerOrderEntity order = null; String xml = message.getMessageBody(); System.out.println("Purchase Order:\n" + xml); try { // Create input source from string containing XML InputSource xmlIS = new InputSource(); xmlIS.setCharacterStream(new StringReader(xml)); // Initialize SAX Builder object SAXBuilder builder = new SAXBuilder(); // Build JDOM Document object from input source Document doc = builder.build(xmlIS); Element rootElem = doc.getRootElement(); // get order id String id = rootElem .getChildText("Id"); // create customer order order = new CustomerOrderEntity(id); // set order date String dateStr = rootElem.getChildText("OrderDate"); order.setOrderDate(DateUtil.getDate(dateStr)); // get company (customer) address element node and parse it Element addrElem = rootElem.getChild("Company"); this.setCompanyAddress(order, addrElem); // get vendor address element node and parse it addrElem = rootElem.getChild("Vendor"); this.setVendorAddress(order, addrElem); // get order items list and parse it Element itemsElem = rootElem.getChild("Items"); this.setOrderItems(order, itemsElem); System.out.println(order.toString()); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } return order; } private void setVendorAddress(CustomerOrderEntity order, Element addrElem) { // get data from xml String coName = addrElem.getChildText("CompanyName"); String streetAddress = addrElem.getChildText("StreetAddress"); String city = addrElem.getChildText("City"); String state = addrElem.getChildText("State"); String zipCode = addrElem.getChildText("ZipCode"); AddressEntity coAddress = order.getVendorAddress(); // build address entity object using data read from xml AddressEntity coAddress = order.getCompanyAddress(); coAddress.setCity(city); coAddress.setCompanyName(coName); coAddress.setStreetAddress(streetAddress); coAddress.setState(state); coAddress.setZipCode(zipCode); } private void setCompanyAddress(CustomerOrderEntity order, Element addrElem) { // get data from xml String coName = addrElem.getChildText("CompanyName"); String streetAddress = addrElem.getChildText("StreetAddress"); String city = addrElem.getChildText("City"); String state = addrElem.getChildText("State"); String zipCode = addrElem.getChildText("ZipCode"); // build address entity object using data read from xml AddressEntity coAddress = order.getCompanyAddress(); coAddress.setCity(city); coAddress.setCompanyName(coName); coAddress.setStreetAddress(streetAddress); coAddress.setState(state); coAddress.setZipCode(zipCode); } private void setOrderItems(CustomerOrderEntity order, Element itemsElem) { List itemList = itemsElem.getChildren(); if (itemList == null) return; // iterate through all items in collection for (int index = 0; index < itemList.size(); index++) { // get current element Element curElem = (Element) itemList.get(index); // get item values String itemId = curElem.getAttributeValue("Id"); String itemName = curElem.getAttributeValue("Name"); String quantity = curElem.getAttributeValue("Quantity"); // create order item CustomerOrderDetailEntity detail = new CustomerOrderDetailEntity(); detail.setItemId(itemId); detail.setItemName(itemName); detail.setQuantity(Integer.parseInt(quantity)); // add order item to order summary order.addItem(detail); } } } |
public OrderSummaryEntity getOrderSummaryForCustomerOrder( CustomerOrderEntity customerOrder) { if (customerOrder == null) return null; // create order OrderSummaryEntity orderSummary = new OrderSummaryEntity(); orderSummary.setOrderDate(new Date()); orderSummary.setReferenceId(customerOrder.getId()); // set addresses orderSummary.setCompanyAddress(customerOrder.getVendorAddress()); orderSummary.setCustomerAddress(customerOrder.getCompanyAddress()); // add items Iterator<CustomerOrderDetailEntity> itemIter = customerOrder.getItems(); while(itemIter.hasNext()) { CustomerOrderDetailEntity item = itemIter.next(); OrderSummaryDetailEntity detail = new OrderSummaryDetailEntity(); detail.setItemId(item.getItemId()); detail.setItemName(item.getItemName()); detail.setQuantity(item.getQuantity()); orderSummary.addItem(detail); } return orderSummary; } |
public class OrderSummarySerializer { public String serializeOrder(OrderSummaryEntity order) { Document doc = new Document(); Element rootElem = new Element("OrderSummary"); doc.addContent(rootElem); // add id Element idElem = new Element("OrderId"); idElem.setText(order.getOrderId()); rootElem.addContent(idElem); // add reference id Element referenceIdElem = new Element("ReferenceId"); referenceIdElem.setText(order.getReferenceId()); rootElem.addContent(referenceIdElem); // add order date Element dateElem = new Element("OrderDate"); String dateStr = DateUtil.getDateString(new Date()); dateElem.setText(dateStr); rootElem.addContent(dateElem); // set company address Element addrElem = this.getElementForAddress("CompanyAddress", order.getCompanyAddress()); rootElem.addContent(addrElem); // set customer address addrElem = this.getElementForAddress("CustomerAddress", order.getCustomerAddress()); rootElem.addContent(addrElem); // iterate through all items and add each item to order Element itemsElem = new Element("Items"); Iterator<OrderSummaryDetailEntity> itemIter = order.getItems(); while(itemIter.hasNext()) { OrderSummaryDetailEntity item = itemIter.next(); Element itemElem = new Element("Item"); itemElem.setAttribute("ItemId", item.getItemId()); itemElem.setAttribute("ItemName", item.getItemName()); String quantityStr = String.valueOf(item.getQuantity()); itemElem.setAttribute("Quantity", quantityStr); itemsElem.addContent(itemElem); } rootElem.addContent(itemsElem); // convert xml document to string XMLOutputter outp = new XMLOutputter(); StringWriter strWriter = new StringWriter(); try { outp.output(doc, strWriter); } catch (IOException e) { e.printStackTrace(); } return strWriter.toString(); } private Element getElementForAddress(String elemName, AddressEntity address) { Element addrElem = new Element(elemName); String coName = address.getCompanyName(); String streetAddress = address.getStreetAddress(); String cityName = address.getCity(); String stateName= address.getState(); String zipCode = address.getZipCode(); Element coElem = new Element("CompanyName"); coElem.setText(coName); addrElem.addContent(coElem); Element streetElem = new Element("StreetAddress"); streetElem.setText(streetAddress); addrElem.addContent(streetElem); Element cityElem = new Element("City"); cityElem.setText(cityName); addrElem.addContent(cityElem); Element stateElem = new Element("State"); stateElem.setText(stateName); addrElem.addContent(stateElem); Element zipCodeElem = new Element("ZipCode"); zipCodeElem.setText(zipCode); addrElem.addContent(zipCodeElem); return addrElem; } } |
protected boolean sendMessage(String queueName, String messageBody) { boolean result = false; try { String accessKeyId = AppConfig.getProperty("AmazonAccessKeyID"); String secretAccessKey = AppConfig.getProperty("AmazonSecretAccessKey"); AmazonSQS service = new AmazonSQSClient(accessKeyId, secretAccessKey); SendMessageRequest request = new SendMessageRequest(); request.setQueueName(queueName); request.setMessageBody(messageBody); SendMessageResponse response = service.sendMessage(request); // Send Message Response if (response.isSetSendMessageResult()) { System.out.println("\tSend Message Result"); SendMessageResult sendMessageResult = response.getSendMessageResult(); if (sendMessageResult.isSetMessageId()) { System.out.println("\t\tMessageId: " + sendMessageResult.getMessageId()); result = true; } } } catch (AmazonSQSException ex) { System.out.println("Caught Exception: " + ex.getMessage()); System.out.println("Response Status Code: " + ex.getStatusCode()); System.out.println("Error Code: " + ex.getErrorCode()); System.out.println("Error Type: " + ex.getErrorType()); System.out.println("Request ID: " + ex.getRequestId()); System.out.println("XML: " + ex.getXML()); } return result; } |
protected boolean deleteMessage(String queueName, String handle) { boolean result = false; try { String accessKeyId = AppConfig.getProperty("AmazonAccessKeyID"); String secretAccessKey = AppConfig.getProperty("AmazonSecretAccessKey"); AmazonSQS service = new AmazonSQSClient(accessKeyId, secretAccessKey); DeleteMessageRequest request = new DeleteMessageRequest(); request.setQueueName(queueName); request.setReceiptHandle(handle); DeleteMessageResponse response = service.deleteMessage(request); // Delete Message Response if (response.isSetResponseMetadata()) { // Response Metadata ResponseMetadata responseMetadata = response.getResponseMetadata(); if (responseMetadata.isSetRequestId()) { System.out.print("RequestId: " + responseMetadata.getRequestId()); result = true; } } } catch (AmazonSQSException ex) { System.out.println("Caught Exception: " + ex.getMessage()); System.out.println("Response Status Code: " + ex.getStatusCode()); System.out.println("Error Code: " + ex.getErrorCode()); System.out.println("Error Type: " + ex.getErrorType()); System.out.println("Request ID: " + ex.getRequestId()); System.out.println("XML: " + ex.getXML()); } return result; } |
(责任编辑:)
如今随着企业的信息化水平不断提高,IT系统的规模在不断的扩大,网络结构也日益复杂,...[详细]
经历了几次跳票之后Fedora 18正式版于2013年1月15日面世,凝聚了许多开发者的心血。很...[详细]
.Net环境,上传处未限制Ashx和Asmx,后者上传无法运行,提示Asmx脚本只能在本地运行,...[详细]
随着网络信息安全的需求度提高,个人信息与网络安全逐渐被提上了台面。人们越来越意识...[详细]
目前互联网上的病毒集团越来越猖狂,对用户的危害也愈演愈烈,大家要懂得保护自己的电...[详细]