06-19-2009 10:12 AM
public void init() {
// Create behaviours
this.onContentUpdate = new JavaBehaviour(this, "onContentUpdate", NotificationFrequency.TRANSACTION_COMMIT);
// Bind behaviours to policies
this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onContentUpdate"),
ContentModel.TYPE_CONTENT, this.onContentUpdate);
Properties properties = new Properties();
properties.load(new FileReader(contextPropFile));
Context ctx = new InitialContext(properties);
Object objHandler = ctx.lookup(this.connectionFactoryJNDI);
this.connectionFactory = (ConnectionFactory)objHandler;
objHandler = ctx.lookup(this.destinationJNDI);
this.destination = (Destination)objHandler;
connection = connectionFactory.createConnection();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
messageProducer = session.createProducer(destination);
}
public void onContentUpdate(NodeRef updatedNodeRef, boolean newContent) {
if (updatedNodeRef != null) {
// Retrieve the document name in its metadata properties
String docName = (String) this.nodeService.getProperty(updatedNodeRef, ContentModel.PROP_NAME);
byte[] documentContent = null;
if (this.sendDocumentContent) {
// Retrieve the document content
ContentReader contentReader = this.contentService.getReader(updatedNodeRef, ContentModel.PROP_CONTENT);
// Then read it in a byte array
ByteArrayOutputStream out = new ByteArrayOutputStream();
contentReader.getContent(out);
documentContent = out.toByteArray();
out.close();
}
if (newContent) {
String creator = (String) this.nodeService.getProperty(updatedNodeRef, ContentModel.PROP_CREATOR);
// The document has been added
sendJMSEvent(updatedNodeRef.getId(), // Document
docName, // name
creator, // Created by username
EVENT_TYPE.DOCUMENT_ADDED,
documentContent); // Document content
}
else {
String modifier = (String) this.nodeService.getProperty(updatedNodeRef, ContentModel.PROP_MODIFIER);
// The document has been updated
sendJMSEvent(updatedNodeRef.getId(), // Document
docName, // name
modifier, // Updated by username
EVENT_TYPE.DOCUMENT_UPDATED,
documentContent); // Document content
}
}
}
}
private void sendJMSEvent(String documentId, String documentName, String user,, byte[] documentContent){
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.setStringProperty(PROP_KEY_DOCUMENT_ID, documentId);
bytesMessage.setStringProperty(PROP_KEY_EVENT_TYPE, event_type.toString());
switch(event_type){
case DOCUMENT_ADDED:
case DOCUMENT_UPDATED:
bytesMessage.setStringProperty(PROP_KEY_DOCUMENT_NAME, documentName);
bytesMessage.setStringProperty(PROP_KEY_DOCUMENT_CREATOR, user);
break;
case DOCUMENT_DELETED:
default:
}
if(this.sendDocumentContent && documentContent != null){
bytesMessage.writeBytes(documentContent);
}
messageProducer.send(bytesMessage);
}
06-19-2009 01:37 PM
06-19-2009 05:03 PM
06-19-2009 05:46 PM
private class JMSSynchronization implements TransactionSynchronization{
public JMSSynchronization(String documentId, String documentName, String user, EVENT_TYPE event_type, byte[] documentContent){
super();
this.documentId = documentId;
this.documentName = documentName;
this.user = user;
this.event_type = event_type;
this.documentContent = documentContent;
}
private String documentId;
private String documentName;
private String user;
private EVENT_TYPE event_type;
private byte[] documentContent;
@Override
public void afterCommit() {}
@Override
public void afterCompletion(int arg0) {
System.out.println("Entering after completion…");
try {
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.setStringProperty(PROP_KEY_DOCUMENT_ID, documentId);
bytesMessage.setStringProperty(PROP_KEY_EVENT_TYPE, event_type.toString());
switch(event_type){
case DOCUMENT_ADDED:
case DOCUMENT_UPDATED:
bytesMessage.setStringProperty(PROP_KEY_DOCUMENT_NAME, documentName);
bytesMessage.setStringProperty(PROP_KEY_DOCUMENT_CREATOR, user);
break;
case DOCUMENT_DELETED:
default:
}
if(sendDocumentContent && documentContent != null){
bytesMessage.writeBytes(documentContent);
}
messageProducer.send(bytesMessage);
}
catch (JMSException e) {
e.printStackTrace();
}
System.out.println("Leaving after completion…");
}
@Override
public void beforeCommit(boolean arg0) {}
@Override
public void beforeCompletion() {}
@Override
public void resume() {}
@Override
public void suspend() {}
}
private void sendJMSEvent(String documentId, String documentName, String user, EVENT_TYPE event_type, byte[] documentContent){
JMSSynchronization jmsSynch = new JMSSynchronization(documentId, documentName, user, event_type, documentContent);
TransactionSynchronizationManager.registerSynchronization(jmsSynch);
}
06-20-2009 08:06 AM
10-16-2012 09:00 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.