So I'm attempting to make a Minecraft chat plugin, here is my code.
public class ChatListener implements Listener {
public static String displayName;
void name(Player event) {
ChatListener.displayName = event.getDisplayName();
}
#EventHandler
public static void (AsyncPlayerChatEvent e) {
final String message = "[" + displayName + "]" + e.getMessage();
e.setMessage(message);
}
}
I always seem to be getting null as displayName in final String message = "[" + displayName + "]" + e.getMessage();
and since I may not have 2 parameters in a variable, how am I supposed to get the display name via getDisplayName()?
If you take a look at AsyncPlayerChatEvent(Spring Javadocs) you'll see that you can access a Player object with it, therefore you don't need to store it in a static variable.
public class ChatListener implements Listener {
#EventHandler
public void onPlayerAsyncChatEvent(AsyncPlayerChatEvent e) {
String displayName = e.getPlayer().getDisplayName(;
final String message = "[" + displayName + "]" + e.getMessage();
e.setMessage(message);
}
}
As you described your problem, this should solve your issue.
Related
I need to queue my json payload in rabbitmq and receive the message from the queue. I can send the message tot he queue fine but when I try to retrieve the message from a topic exchange I get the null pointer exception. following line in onMessage method implementation throwing the null pointer exception.
simpleMessage=(SimpleMessage) rabbitTemplate.receiveAndConvert(qu);
Can someone explain me how to do it or guide me to the link that has the solution
#Component
public class RabbitListener implements me {
#Autowired
RabbitTemplate rabbitTemplae;
//#Override
public void onMessage(Message message) {
System.out.println("in listener");
SimpleMessage simpleMessage= new SimpleMessage();
// System.out.println("Received a new message = [" + new String(message.getBody().toString()) + "]");
System.out.println("READY TO RECEIVE");
String qu=message.getMessageProperties().getConsumerQueue();
System.out.println("que name "+qu);
//simpleMessage=(SimpleMessage) rabbitTemplae.receiveAndConvert("MyTestQueue");
//System.out.println("rabbitMQ RECEIVE MESSAGE "+ rabbitTemplae.receiveAndConvert("MyTestQueu", 100000));
simpleMessage=(SimpleMessage) rabbitTemplae.receiveAndConvert(qu);
//rabbitTemplae.receiveAndConvert
// System.out.println( simpleMessage.getDescription());
}
}
need to convert it back to the following object from the queue
public class SimpleMessage implements Serializable {
private String name;
private String description;
public SimpleMessage() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Override
public String toString() {
return "SimpleMessage{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
When I was trying to implement Delta Propagation in GemFire, I was getting Exception shown below...
Exception...
com.gemstone.gemfire.pdx.PdxSerializationException: Could not create an instance of a class DeltaTesting with root cause
java.lang.ClassNotFoundException: DeltaTesting
at org.apache.geode.internal.ClassPathLoader.forName(ClassPathLoader.java:437)
at org.apache.geode.internal.InternalDataSerializer.getCachedClass(InternalDataSerializer.java:4010)
at org.apache.geode.pdx.internal.PdxType.getPdxClass(PdxType.java:235)
at org.apache.geode.pdx.internal.PdxReaderImpl.basicGetObject(PdxReaderImpl.java:687)
at org.apache.geode.pdx.internal.PdxReaderImpl.getObject(PdxReaderImpl.java:682)
Code...
#Region("deltaTesting")
public class DeltaTesting implements Delta,Serializable {
private static final long serialVersionUID = 1L;
public DeltaTesting(){
}
public DeltaTesting(String id, String firstName, String lastName){
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
#Id
private String id;
private String firstName;
private String lastName;
private transient boolean stringId = false;
private transient boolean stringFirstName = false;
private transient boolean stringLastName = false;
public String getId() {
return id;
}
public void setId(String id) {
this.stringId = true;
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.stringFirstName = true;
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.stringLastName = true;
this.lastName = lastName;
}
#Override
public void fromDelta(DataInput in) throws IOException, InvalidDeltaException {
// TODO Auto-generated method stub
System.out.println("Applying delta to " + this.toString());
// For each field, read whether there is a change
if (in.readBoolean()) {
// Read the change and apply it to the object
this.id = in.readLine();
System.out.println(" Applied delta to field 'id' = "
+ this.id);
}
if (in.readBoolean()) {
// Read the change and apply it to the object
this.firstName = in.readLine();
System.out.println(" Applied delta to field 'firstname' = "
+ this.firstName);
}
if (in.readBoolean()) {
// Read the change and apply it to the object
this.lastName = in.readLine();
System.out.println(" Applied delta to field 'lastname' = "
+ this.lastName);
}
}
#Override
public boolean hasDelta() {
// TODO Auto-generated method stub
return this.stringId || this.stringFirstName || this.stringLastName;
}
#Override
public void toDelta(DataOutput out) throws IOException {
out.writeBoolean(stringId);
// TODO Auto-generated method stub
if (stringId) {
out.writeBytes(this.id);
this.stringId = false;
System.out.println(" Extracted delta from field 'id' = " + this.id);
}
out.writeBoolean(stringFirstName);
if (stringFirstName) {
out.writeBytes(this.firstName);
this.stringFirstName = false;
System.out.println(" Extracted delta from field 'firstName' = " + this.firstName);
}
out.writeBoolean(stringLastName);
if (stringLastName) {
out.writeBytes(this.lastName);
this.stringLastName = false;
System.out.println(" Extracted delta from field 'lastName' = " + this.lastName);
}
}
}
**cache.xml**
<pdx>
<pdx-serializer>
<class-name>com.gemstone.gemfire.pdx.ReflectionBasedAutoSerializer</class-
name>
<parameter name="classes">
<string>com\.xxx\..+</string>
</parameter>
</pdx-serializer>
</pdx>
#Vigneshwara-
Delta Propagation is not supported by PDX Serialization in GemFire. You must use the GemFire Data Serialization framework (i.e. DataSerializable and DataSerializers) with Deltas instead.
In short... if you use PDX, then you cannot use Deltas. If you use Deltas, then you cannot use PDX.
Sorry,
-j
I have two packages, one for client and one for server. The Message.java file is present in both packages.
// Message.java
import java.io.Serializable;
public class Message implements Serializable{
private static final long serialVersionUID = 1L; //*** This line is present only on server side.
public String type, sender, content, recipient;
public Message(String type, String sender, String content, String recipient){
this.type = type;
this.sender = sender;
this.content = content;
this.recipient = recipient;
}
public String toString(){
return "{type='"+type+"', sender='"+sender+"', content='"+content+"', recipient='"+recipient+"'}";
}
}
On Client side i have the following code
public void send(Message msg){
try{
Out.writeObject(msg);
Out.flush();
}catch(Exception e){
System.out.println("Exception SocketClient send()");
}
}
On server side i have the following code
public void run(){
while (true){
try{
Message msg = (Message) streamIn.readObject();
}
catch(Exception ioe){
System.out.println(ID + " ERROR reading: " + ioe.getMessage());
}
}
}
On line "Message msg = (Message) streamIn.readObject();" on server side iam getting
"ClassCastException". Please help, as i am stuck here !
I have a field set pointcut, which seems to do as I expect. Its defined as follows
before(Object newval): set(#Serviced private * *.*) && args(newval)
The above is meant to capture: whenever a private field attribute, annotated with #Serviced, is set call my before advice.
Everything seems to work fine, except for the one case in my code that sets a variable matching the above via java reflection ( ie via java.lang.reflect.Field.set(....).
Any idea's how I can catch that "set" also?
Thanks
As you have noticed, the set() pointcut cannot intercept reflective field changes. But if you control (i.e. can weave aspects into) the code calling the Field.set*(..) methods, you can work around that issue by also using reflection. Here is a complete, compileable code sample illustrating the solution:
Sample annotation:
package de.scrum_master.app;
import java.lang.annotation.*;
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.FIELD)
public #interface Serviced {}
Sample entity class with main method:
package de.scrum_master.app;
public class Person {
#Serviced private int id;
#Serviced private String name;
private String country;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getCountry() { return country; }
public void setCountry(String country) { this.country = country; }
public void setIdReflective(int id) throws Exception {
Person.class.getDeclaredField("id").setInt(this, id);
}
public void setNameReflective(String name) throws Exception {
Person.class.getDeclaredField("name").set(this, name);
}
public void setCountryReflective(String country) throws Exception {
Person.class.getDeclaredField("country").set(this, country);
}
#Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", country=" + country + "]";
}
public static void main(String[] args) throws Exception {
Person person = new Person();
person.setId(11);
person.setName("Tin Man");
person.setCountry("Oz");
System.out.println("Before reflective setters: " + person);
person.setIdReflective(22);
person.setNameReflective("Cowardly Lion");
person.setCountryReflective("The Land of Oz");
System.out.println("After reflective setters: " + person);
}
}
As you can see, only two out of three private fields have the #Serviced annotation. Setters are called for all three fields twice: once normally and once via reflection.
Aspect intercepting both normal and reflective field changes:
package de.scrum_master.aspect;
import de.scrum_master.app.Serviced;
import java.lang.reflect.Field;
public aspect ServicedFieldChangeInterceptor {
before(Object newValue):
set(#Serviced private * *) && args(newValue)
{
System.out.println(thisJoinPointStaticPart + " -> " + newValue);
}
before(Object newValue, Field field):
call(public void Field.set*(Object, *)) && args(*, newValue) && target(field)
{
if (field.getAnnotation(Serviced.class) == null)
return;
System.out.println(thisJoinPointStaticPart + " -> " + field + ", " + newValue);
}
}
Sample output when running Person.main:
set(int de.scrum_master.app.Person.id) -> 11
set(String de.scrum_master.app.Person.name) -> Tin Man
Before reflective setters: Person [id=11, name=Tin Man, country=Oz]
call(void java.lang.reflect.Field.setInt(Object, int)) -> private int de.scrum_master.app.Person.id, 22
call(void java.lang.reflect.Field.set(Object, Object)) -> private java.lang.String de.scrum_master.app.Person.name, Cowardly Lion
After reflective setters: Person [id=22, name=Cowardly Lion, country=The Land of Oz]
The output clearly shows that both advice only "do something" (in this case print information to standard output) for fields annotated with #Serviced, whereas other fields are skipped. While the set() pointcut applies statically, the reflective one needs to determine if the target field has a matching annotation dynamically.
Why isn't this working for me?
public class Band {
public void addMember(Musician musician) {
musicians.add(musician);
System.out.println("Muscian: " + musician + "was successfully added");
}
}
public static void main(String[] args) {
Band Beatles = new Band("Beatles");
Beatles.addMember("John Lennon");
}
public class Musician {
private String name;
public Musician(String name, Instrument instrument) {
this.name = name;
Instrument Guitar = instrument;
}
public void play() {
}
}
Beatles.addMember("John Lennon");
should be
Beatles.addMember(new Musician("John Lennon", new Instrument(new Guitar()));
I suspect, without seeing your actual Instrument class, based on your comment that this line should work.
The problem is that you are treating some objects as Strings. You need to create the object out of the class that defines it first.